-
Notifications
You must be signed in to change notification settings - Fork 58
/
Day05.java
65 lines (52 loc) · 1.08 KB
/
Day05.java
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
63
64
65
package com.offer;
/**
* 快速排序
* @author kexun
*
*/
public class Day05 {
private static int count = 0;
public static void main(String[] args) {
int[] array = {5,4,3,2,1};
Day05 d = new Day05();
array = d.partition(array, 0, array.length - 1);
// for (int a : array) {
// System.out.println(a);
// }
System.out.println(array.length);
System.out.println(count);
}
public int[] partition(int[] array, int i, int j) {
if (j - i <= 0 ) {
return array;
}
int start = i;
int end = j;
int base = array[start];
while (start < end) {
// count++;
// 先从右向左找小于基数的值
while (start < end && array[end] >= base) {
count++;
end--;
}
if (start < end) {
array[start] = array[end];
start++;
}
// 先从左向右找小大于基数的值
while (start < end && array[start] < base) {
count++;
start++;
}
if (start < end) {
array[end] = array[start];
end--;
}
}
array[start] = base;
partition(array, i, start - 1);
partition(array, end + 1, j);
return array;
}
}