快速排序算法
package Alg; import java.util.Random; public class QuickSort { public static void quickSort(int[] ary) { quickSort(ary, 0, ary.length - 1); } /** * @param ary 数组 排序数组 * @param start 开始位置 ,0-base index * @param end 结束位置,0-base index */ private static void quickSort(int[] ary, int start, int end) { if (start < end) { int p = partition(ary, start, end); quickSort(ary, start, p - 1); quickSort(ary, p + 1, end); } } private static int partition(int[] ary, int start, int end) { int pivot = start, i = start, j = start + 1; for (; j <= end; j++) { if (ary[j] < ary[pivot]) { i++; if (i == j) continue; int temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; } } int temp = ary[pivot]; ary[pivot] = ary[i]; ary[i] = temp; return i; } public static void testCase1() { int length = 15; // int[] ary = new int[] {956,683,804,133,174,595,483,834,713,433,827,253,752,62,73}; int[] ary = new int[length]; java.util.Random rand = new Random(); for (int i = 0; i < length; i++) { ary[i] = Math.abs(rand.nextInt()) % 1000; } for (int i = 0; i < length; i++) System.out.print(ary[i] + ","); System.out.println(); quickSort(ary); for (int i = 0; i < length; i++) System.out.print(ary[i] + ","); System.out.println(); } }