写java冒泡排序代码
时间: 2023-05-30 08:05:06 浏览: 53
以下是Java实现冒泡排序的代码:
```
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换相邻元素
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
```
在这个实现中,我们循环n-1次,每次都比较相邻的两个元素,如果它们的顺序不对,则交换它们。在每一次内层循环中,我们都会将当前未排序的元素中的最大值移动到最后一个位置。这样,经过n-1次外层循环后,整个数组就被排序了。
相关问题
写出java冒泡排序代码
### 回答1:
Java冒泡排序代码如下:
public class BubbleSort {
public static void main(String[] args) {
int[] array = {1, 4, 7, 8, 10, 15};
int temp;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
System.out.println("冒泡排序后:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
### 回答2:
冒泡排序是一种简单的排序算法,它的基本思想是通过相邻元素之间的比较和交换来实现排序。
下面是用Java实现冒泡排序的代码:
```java
public class BubbleSort {
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
// 每一轮找出一个最大值,因此比较的次数会逐渐减少
for (int j = 0; j < n - 1 - i; j++) {
// 相邻元素比较
if (array[j] > array[j + 1]) {
// 若前面的元素大于后面的元素,则交换位置,将大的元素向后移动
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
// 测试代码
public static void main(String[] args) {
int[] array = { 5, 2, 4, 6, 1, 3 };
bubbleSort(array);
for (int num : array) {
System.out.print(num + " ");
}
}
}
```
以上代码通过两层循环实现了冒泡排序。外层循环控制比较的轮数,内层循环进行相邻元素的比较和交换。每一轮比较将最大的元素移动到末尾,因此可以逐步缩小比较范围。最终得到一个有序的数组。
上述代码中给出了一个简单的测试例子,通过调用`bubbleSort`方法对数组进行排序,然后输出结果。输出结果为:1 2 3 4 5 6,对应排序后的数组。
### 回答3:
Java冒泡排序代码如下:
```java
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换arr[j]和arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("排序后的数组:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
```
以上代码为一个在Java中实现的冒泡排序算法。冒泡排序是一种简单且低效的排序算法,通过交换相邻元素的位置来将较大的元素往后移动。复杂度为O(n^2),在实际应用中通常不推荐使用。
Java 写段冒泡排序代码
### 回答1:
当然可以,下面是Java语言实现的冒泡排序代码:
```
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
```
这段代码会对传入的整型数组 `arr` 进行冒泡排序。冒泡排序的原理是每次将未排序的最大值冒泡到已排序的最后面。
### 回答2:
冒泡排序(Bubble Sort)是一种简单的排序算法,它重复地比较相邻的两个元素,将较大的元素往后移动,较小的元素往前移动。通过多次遍历,直到所有元素有序为止。
以下是用Java编写的冒泡排序代码:
```java
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换 arr[j] 和 arr[j+1] 的值
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("排序前的数组:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
bubbleSort(arr);
System.out.println("\n排序后的数组:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
```
以上代码中,我们定义了一个 `bubbleSort` 方法来执行冒泡排序。`bubbleSort` 方法中使用两个嵌套的循环,外层循环控制遍历次数,内层循环用于比较和交换相邻元素。
在 `main` 方法中,我们创建了一个整型数组并赋初值,然后通过调用 `bubbleSort` 方法对数组进行排序,并输出排序前后的结果。
运行以上代码,输出结果如下:
```
排序前的数组:
64 34 25 12 22 11 90
排序后的数组:
11 12 22 25 34 64 90
```
### 回答3:
冒泡排序是一种简单的排序算法,通过多次遍历数组,比较相邻元素的大小并交换位置,从而将较大(或较小)的元素逐渐“冒泡”到数组的一端。下面是使用Java语言写的冒泡排序代码:
```
public class BubbleSort {
public static void main(String[] args) {
int[] array = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(array);
System.out.println("排序后的数组:");
for (int i : array) {
System.out.print(i + " ");
}
}
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (array[j] > array[j + 1]) {
// 交换相邻元素的位置
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
```
上述代码定义了一个名为`BubbleSort`的类,其中`main`方法是程序的入口。在`main`方法中,初始化一个包含一些随机整数的数组`array`。然后,调用`bubbleSort`方法对数组进行排序。最后,打印排序后的数组元素。
`bubbleSort`方法采用两层嵌套循环实现冒泡排序过程。外层循环控制遍历数组的轮数,内层循环用于比较相邻元素并进行位置交换。如果当前元素比下一个元素大,就互换它们的位置。每完成一轮内层循环,最大的元素将“冒泡”到数组的末尾。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)