forked from hollischuang/toBeTopJavaer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuickSort.md
More file actions
55 lines (42 loc) · 1.19 KB
/
Copy pathQuickSort.md
File metadata and controls
55 lines (42 loc) · 1.19 KB
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
```java
public class QuickSort {
public static void main(String[] args) {
int[] arr = {5, 1, 4, 11, 2, 22, 1, 33, 44, 1, 0, -1, 11};
QuickSort sort = new QuickSort();
sort.sort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public void sort(int[] arr, int left, int right) {
if (left < right) {
int pivot = partition(arr, left, right);
sort(arr, left, pivot - 1);
sort(arr, pivot + 1, right);
}
}
public int partition(int[] arr, int left, int right) {
int midValue = arr[left];
int i = left, j = right + 1;
while (true) {
do {
i++;
} while (i <= right && arr[i] < midValue);
do {
j--;
} while (j >= 0 && arr[j] > midValue);
if (i >= j) {
break;
}
swap(arr, i, j);
}
swap(arr, left, j);
return j;
}
public void swap(int[] arr, int left, int right) {
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
}
```