冒泡排序

冒泡排序算法


public class BubbleSort {
public void bubbleSort(Integer[] arr) {
for (int i = 0; i < arr.length; i++) {
boolean flag = false;
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp;
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
flag = true;
}
}
if (!flag) break;
}
}


public static void main(String[] args) {
Integer arr[] = {2, 4, 7, 6, 8, 5, 9};
BubbleSort bubbleSort = new BubbleSort();
bubbleSort.bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
}

posted @ 2019-09-02 11:41  云vs成vs鹏  阅读(84)  评论(0编辑  收藏  举报