|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 |
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(); }} |
原文:http://www.cnblogs.com/northhurricane/p/3612574.html