-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortTest.java
More file actions
50 lines (36 loc) · 1018 Bytes
/
QuickSortTest.java
File metadata and controls
50 lines (36 loc) · 1018 Bytes
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
package com.chen.test;
import org.junit.Test;
/**
* @author : chen weijie
* @Date: 2020-04-27 00:15
*/
public class QuickSortTest {
@Test
public void solution() {
int[] array = {4, 3, 1, 0, 6, 5, 10};
sort(array, 0, array.length - 1);
}
public void sort(int[] array, int low, int high) {
Integer pivot = getPivot(array, low, high);
sort(array, low, pivot - 1);
sort(array, pivot + 1, high);
for (int num : array) {
System.out.print(num);
}
}
private int getPivot(int[] array, int low, int high) {
int pivotValue = array[low];
while (low < high) {
while (low < high && array[high] >= pivotValue) {
--high;
}
array[low] = array[high];
while (low < high && array[low] <= pivotValue) {
++low;
}
array[high] = array[low];
}
array[low] = pivotValue;
return low;
}
}