-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
81 lines (69 loc) · 2.27 KB
/
QuickSort.java
File metadata and controls
81 lines (69 loc) · 2.27 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Arrays;
import java.util.Stack;
/**
* Author: dc0453
* Since: 2017/3/10 下午2:50
* quickSort
*/
public class QuickSort {
private int partition(int[] array, int low, int high) {
int pivot = array[low];
while (low < high) {
while (low < high && pivot < array[high]) {
high--;
}
array[low] = array[high];
while (low < high && array[low] < pivot) {
low++;
}
array[high] = array[low];
}
array[low] = pivot;
return low;
}
private void quickSortRecursively(int[] array, int low, int high) {
if (low < high) {
int pivotIndex = partition(array, low, high);
quickSortRecursively(array, low, pivotIndex - 1);
quickSortRecursively(array, pivotIndex + 1, high);
}
}
private void quickSortNonRecursively(int[] array, int low, int high) {
Stack<Integer> stack = new Stack<>();
if (low < high) {
int pivotIndex = partition(array, low, high);
if (pivotIndex - 1 > low) {
stack.push(low);
stack.push(pivotIndex - 1);
}
if (pivotIndex + 1 < high) {
stack.push(pivotIndex + 1);
stack.push(high);
}
while (!stack.empty()) {
high = stack.pop();
low = stack.pop();
pivotIndex = partition(array, low, high);
if (pivotIndex - 1 > low) {
stack.push(low);
stack.push(pivotIndex - 1);
}
if (pivotIndex + 1 < high) {
stack.push(pivotIndex + 1);
stack.push(high);
}
}
}
}
public void quickSort(int[] array) {
// quickSortRecursively(array, 0, array.length - 1);
quickSortNonRecursively(array, 0, array.length - 1);
}
public static void main(String[] args) {
QuickSort quickSort = new QuickSort();
int[] numbers = {10,20,15,0,6,7,2,1,-5,55};
System.out.println(Arrays.toString(numbers));
quickSort.quickSort(numbers);
System.out.println(Arrays.toString(numbers));
}
}