public class Sort { public static void main(String[] args) { int[] a = { 8, 7, 3, 5, 4, 2, 1, 6 }; //bubbleSort2(a); //print(a); mineQuickSort(a, 0, a.length-1); display(a); } private static void display(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } /** * 直接插入排序 * * @param a * wyq */ public static void insertSort(int[] a) { int temp; int n = a.length; for (int i = 0; i < n - 1; i++) { temp = a[i + 1]; int j = i; while (j > -1 && a[j] > temp) { a[j + 1] = a[j]; j--; } a[j + 1] = temp; } } /** * 直接选择排序 * * @param a * wyq */ public static void selectSort(int[] a) { int i, j, small; int temp; int n = a.length; for (i = 0; i < n; i++) { small = i; for (j = i + 1; j < n; j++) if (a[j] < a[small]) small = j; if (small != i) { temp = a[i]; a[i] = a[small]; a[small] = temp; } } } /** * 冒泡排序1 * * @param a * wyq */ public static void bubbleSort(int[] a) { int n = a.length; int temp; for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { if (a[j] < a[j - 1]) { temp = a[j]; a[j] = a[j - 1]; a[j - 1] = temp; } } } } /** * 冒泡排序2 * * @param a * wyq */ public static void bubbleSort2(int[] a) { int n = a.length; int temp, flag = 1; for (int i = 1; i < n && flag == 1; i++) { flag = 0; for (int j = 0; j < n - i; j++) { if (a[j] > a[j + 1]) { flag = 1; temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } public static void quickSort(int[] a, int low, int high) { int i, j; int temp; i = low; j = high; temp = a[low]; while (i < j) { // 在数组的右端扫描 while (i < j && temp <= a[j]) j--; if (i < j) { a[i] = a[j]; i++; } // 在数组的左端扫描 while (i < j && a[i] < temp) i++; if (i < j) { a[j] = a[i]; j--; } } a[i] = temp; if (low < i) quickSort(a, low, i - 1); // 对左端子集合递归 if (i < high) quickSort(a, j + 1, high); // 对右端子集合递归 } /** * * @param a * @param left * @param right * 中值 */ public static int partition(int[] a, int left, int right) { int temp = a[left]; while (lefta[left]) left++; a[right] = a[left]; } a[left] = temp; return left; } /** * 另一种快速排序 * @param a * @param left * @param right */ public static void mineQuickSort(int[] a, int left, int right) { if (left
研究算法真是锻炼人的逻辑思维能力。