冒泡排序

2019-09-29  本文已影响0人  梁女神超过他

冒泡排序源代码

package sort;

public class BubleSort {

public static void main(String[] args) {

int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };

int[] a = bubbleSort(arr);

for (int b : a) {

System.out.println(b);

}

}

public static int[] bubbleSort(int arr[]) {

if (arr.length <= 1) {

return arr;

}

for (int i = 0; i < arr.length - 1; i++) {

for (int j = 0; j < arr.length - 1 - i; j++) {

int temp = 0;

if (arr[j] > arr[j + 1]) {

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}

}

}

return arr;

}

}

上一篇 下一篇

猜你喜欢

热点阅读