冒泡排序

//冒泡排序
//冒泡排序要经过N-1步的中间排序才能排完,效率较低
public class BublleSort {
public static void main(String[] args) {
int[] arr = {1, 56, 36, 74, 2, 6, 3, 21, 5};
bubbleSort(arr);
System.out.print("排序过后的数组:" + Arrays.toString(arr));
}

public static void bubbleSort(int[] arr) {
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
//相比较然后两数交换
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

}
排序过后的数组:[1, 2, 3, 5, 6, 21, 36, 56, 74]
posted @ 2019-04-27 14:57  jason小蜗牛  阅读(182)  评论(0编辑  收藏  举报