美文网首页
冒泡排序

冒泡排序

作者: 梁女神超过他 | 来源:发表于2019-09-29 16:05 被阅读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;

}

}

相关文章

网友评论

      本文标题:冒泡排序

      本文链接:https://www.haomeiwen.com/subject/qtofpctx.html