向大端冒泡
public class BubbleSort { public static <T extends Comparable<? super T>> void sort(T[] arr) { for (int i = 0, len = arr.length; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { if (arr[j].compareTo(arr[j + 1]) > 0) { swap(arr, j, j + 1); } } } } private static void swap(Object[] arr, int i, int j) { if (i != j) { Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } private static void printArr(Object[] arr) { for (Object o : arr) { System.out.print(o); System.out.print("\t"); } System.out.println(); } public static void main(String args[]) { Integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printArr(arr);//3 5 1 7 2 9 8 0 4 6 sort(arr); printArr(arr);//0 1 2 3 4 5 6 7 8 9 } }
向小端冒泡
public class BubbleSort { public static <T extends Comparable<? super T>> void sort(T[] arr) { for (int i = 0, len = arr.length; i < len - 1; i++) { for (int j = len - 1; j > i; j--) { if (arr[j-1].compareTo(arr[j])>0) { swap(arr,j - 1, j); } } } } private static void swap(Object[] arr, int i, int j) { if (i != j) { Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } private static void printArr(Object[] arr) { for (Object o : arr) { System.out.print(o); System.out.print("\t"); } System.out.println(); } public static void main(String args[]) { Integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printArr(arr);//3 5 1 7 2 9 8 0 4 6 sort(arr); printArr(arr);//0 1 2 3 4 5 6 7 8 9 } }
雞尾酒排序(來回排序)
例子來自百度:以序列(2,3,4,5,1)為例,雞尾酒排序只需要訪問兩次(升序降序各一次 )次序列就可以完成排序,但如果使用冒泡排序則需要四次。
普通冒泡和雞尾酒都是交換了4次,但是雞尾酒是遍歷了2遍數組,也就是讀取了10個數;而冒泡排序遍歷了4遍數組,也就是讀取了20個數。
public class BubbleSort { public static <T extends Comparable<? super T>> void sort(T[] arr) { int low = 0;//low前面的已經排好序 int high = arr.length - 1;//high后面的已經排好序 while (low < high) {//倆指針相遇說明排序完畢 //正向冒泡 for (int i = low; i < high; i++) { if (arr[i].compareTo(arr[i + 1]) > 0) { swap(arr, i, i + 1); } } high--; //反向冒泡 for (int j = high; j > low; j--) { if (arr[j - 1].compareTo(arr[j]) > 0) { swap(arr, j - 1, j); } } low++; } } private static void swap(Object[] arr, int i, int j) { if (i != j) { Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } private static void printArr(Object[] arr) { for (Object o : arr) { System.out.print(o); System.out.print("\t"); } System.out.println(); } public static void main(String args[]) { Integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printArr(arr);//3 5 1 7 2 9 8 0 4 6 sort(arr); printArr(arr);//0 1 2 3 4 5 6 7 8 9 } }
冒泡優化1:用標記位提前判斷有序性
在數組基本有序時,如果經過少數趟冒泡后,發現已經順序了,則不必循環完n-1次才結束,這時已經可以立即停止排序了。
public class BubbleSort { public static <T extends Comparable<? super T>> void sort(T[] arr) { for (int i = 0, len = arr.length; i < arr.length; i++) { boolean hasChanged = false; for (int j = 0; j < len - i - 1; j++) { if (arr[j].compareTo(arr[j + 1]) > 0) { swap(arr, j, j + 1); hasChanged = true; } } if(!hasChanged) break; } } private static void swap(Object[] arr, int i, int j) { if (i != j) { Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } private static void printArr(Object[] arr) { for (Object o : arr) { System.out.print(o); System.out.print("\t"); } System.out.println(); } public static void main(String args[]) { Integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printArr(arr);//3 5 1 7 2 9 8 0 4 6 sort(arr); printArr(arr);//0 1 2 3 4 5 6 7 8 9 } }
冒泡優化2:記錄下最后一次交換的位置j,表示j后面已經排好序
隨時地變更i,即還需要遍歷的次數在不斷變更。普通冒泡排序每一趟排序只能減少1個數字的遍歷規模(也就是i++),但是記錄最后交換位置后,每次可以減少多個數字(也就是i = len - lastPosition - 1)。
public class BubbleSort { public static <T extends Comparable<? super T>> void sort(T[] arr) { for (int i = 0, len = arr.length, lastPosition = 0; i < len - 1; i = len - lastPosition - 1) { lastPosition = 0; for (int j = 0; j < len - i - 1; j++) { if (arr[j].compareTo(arr[j + 1]) > 0) { swap(arr, j, j + 1); lastPosition = j; } } } } private static void swap(Object[] arr, int i, int j) { if (i != j) { Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } private static void printArr(Object[] arr) { for (Object o : arr) { System.out.print(o); System.out.print("\t"); } System.out.println(); } public static void main(String args[]) { Integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printArr(arr);//3 5 1 7 2 9 8 0 4 6 sort(arr); printArr(arr);//0 1 2 3 4 5 6 7 8 9 } }