java完整的冒泡排序代码
时间: 2023-02-28 09:54:12 浏览: 54
public class BubbleSort {
public static void main(String[] args) {
int[] array = {20, 15, 12, 30, -5, 72, 456};
bubbleSort(array);
System.out.println("排序后的数组:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
} public static void bubbleSort(int[] array) {
int temp;
for (int i = 0; i < array.length; 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;
}
}
}
}
} 答案: Java完整的冒泡排序代码如下:public class BubbleSort { public static void main(String[] args) { int[] array = {20, 15, 12, 30, -5, 72, 456}; bubbleSort(array); System.out.println("排序后的数组:"); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } public static void bubbleSort(int[] array) { int temp; for (int i = 0; i < array.length; 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; } } } } }
相关推荐
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)