@@ -10,35 +10,98 @@ private static void quickSort(int[] nums, int low, int high) {
10
10
if (low >= high ) {
11
11
return ;
12
12
}
13
+ //取得中轴元素所在的中轴区间
13
14
int [] p = partition (nums , low , high );
14
15
quickSort (nums , low , p [0 ] - 1 );
15
- quickSort (nums , p [0 ] + 1 , high );
16
+ quickSort (nums , p [1 ] + 1 , high );
16
17
}
17
18
18
19
private static int [] partition (int [] nums , int low , int high ) {
19
- int less = low - 1 , more = high ;
20
+ int less = low - 1 , more = high , pivotal = nums [ high ] ;
20
21
while (low < more ) {
21
- if (nums [low ] < nums [ high ] ) {
22
+ if (nums [low ] < pivotal ) {
22
23
swap (nums , ++less , low ++);
23
- } else if (nums [low ] > nums [ high ] ) {
24
+ } else if (nums [low ] > pivotal ) {
24
25
swap (nums , --more , low );
25
26
} else {
26
27
++low ;
27
28
}
28
29
}
29
30
swap (nums , more , high );
31
+ //返回中轴元素所对应的区间
30
32
return new int [] {less + 1 , more };
31
33
}
32
34
33
35
private static void swap (int [] nums , int i , int j ) {
36
+ if (i == j ) {
37
+ return ;
38
+ }
34
39
int t = nums [i ];
35
40
nums [i ] = nums [j ];
36
41
nums [j ] = t ;
37
42
}
38
43
39
44
public static void main (String [] args ) {
40
- int [] nums = {1 , 2 , 7 , 4 , 5 , 3 };
41
- quickSort (nums );
45
+ int [] nums = {1 , 2 , 7 , 4 , 5 , 3 ,3 ,4 ,5 ,6 ,7 ,3 ,0 };
46
+ //quickSort(nums);
47
+ myQuickSort (nums );
48
+ System .out .println (Arrays .toString (nums ));
49
+ }
50
+ public static void myQuickSort (int [] nums ) {
51
+ if (nums == null || nums .length == 1 ) {
52
+ return ;
53
+ }
54
+ myQuickSort (nums , 0 ,nums .length -1 );
55
+
56
+ }
57
+ private static void myQuickSort (int [] nums , int low , int high ) {
58
+ if (low >= high ) {
59
+ return ;
60
+ }
61
+ int [] partition = myPartition (nums , low , high );
62
+ System .out .println ("low:" +low +",high:" +high +"本次分区后的数组为" );
42
63
System .out .println (Arrays .toString (nums ));
64
+ myQuickSort (nums , low , partition [0 ]-1 );
65
+ myQuickSort (nums , partition [1 ]+1 , high );
66
+ }
67
+ private static int [] myPartition (int [] nums , int low , int high ) {
68
+ int k =low + (high -low )/2 ;
69
+ int less = low , more = high , pivotal = nums [k ];
70
+ while (low <= more ) {
71
+ if (nums [low ] < pivotal ) {
72
+ swap (nums , less ++, low ++);
73
+ } else if (nums [low ] > pivotal ) {
74
+ swap (nums , more --, low );
75
+ } else {
76
+ low ++;
77
+ }
78
+ }
79
+ System .out .println ("low:" +low +",high:" +high +"本次分区后的数组为" +"less:" +less +",more:" +more );
80
+ if (low <=more ){
81
+ swap (nums , more , k );
82
+ }
83
+ return new int []{less , more };
43
84
}
85
+ //low:6,high:12本次分区后的数组为less:3,more:5
86
+ //low:0,high:12本次分区后的数组为
87
+ //[1, 2, 0, 3, 3, 3, 4, 5, 6, 7, 5, 4, 7]
88
+ //low:3,high:2本次分区后的数组为less:2,more:2
89
+ //low:0,high:2本次分区后的数组为
90
+ //[1, 0, 2, 3, 3, 3, 4, 5, 6, 7, 5, 4, 7]
91
+ //low:2,high:1本次分区后的数组为less:1,more:1
92
+ //low:0,high:1本次分区后的数组为
93
+ //[0, 1, 2, 3, 3, 3, 4, 5, 6, 7, 5, 4, 7]
94
+ //low:13,high:12本次分区后的数组为less:11,more:12
95
+ //low:6,high:12本次分区后的数组为
96
+ //[0, 1, 2, 3, 3, 3, 4, 5, 6, 5, 4, 7, 7]
97
+ //low:11,high:10本次分区后的数组为less:10,more:10
98
+ //low:6,high:10本次分区后的数组为
99
+ //[0, 1, 2, 3, 3, 3, 4, 5, 5, 4, 6, 7, 7]
100
+ //low:10,high:9本次分区后的数组为less:8,more:9
101
+ //low:6,high:9本次分区后的数组为
102
+ //[0, 1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7]
103
+ //low:8,high:7本次分区后的数组为less:6,more:7
104
+ //low:6,high:7本次分区后的数组为
105
+ //[0, 1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7]
106
+ //[0, 1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7]
44
107
}
0 commit comments