链接:http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html
Java代码实现:
public class Sort { static final int MAX = 20; public static void main(String[] args) { int[] data = new int[MAX]; Random random = new Random(); // 生成一个随机数组 for (int i = 0; i < MAX; i++) { data[i] = random.nextInt(MAX * MAX); } // 打印排序前数据内容 System.out.println(Arrays.toString(data)); quickSort(data, 0, MAX - 1); // 打印排序后的数组 System.out.println(Arrays.toString(data)); } static void quickSort(int[] data, int p, int q) { if (p < q) { int r = partition(data, p, q); quickSort(data, p, r - 1); quickSort(data, r + 1, q); } } static int partition(int[] data, int p, int q) { int pvt = q, i = p - 1; int temp; for (int j = i + 1; j < q; j++) { if (data[j] < data[pvt]) { temp = data[j]; data[j] = data[++i]; data[i] = temp; } } temp = data[++i]; data[i] = data[pvt]; data[pvt] = temp; return i; } }