-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathQuickSort.java
36 lines (32 loc) · 1021 Bytes
/
QuickSort.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
package algorithm;
public class QuickSort {
public static void quickSort(int[] arr, int begin, int end) {
if (begin < end) {
int partitionIndex = partition(arr, begin, end);
quickSort(arr, begin, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, end);
}
}
private static int partition(int[] arr, int begin, int end) {
int pivot = arr[begin];
while (begin < end) {
while (begin < end && arr[end] >= pivot) {
end--;
}
arr[begin] = arr[end];
while (begin < end && arr[begin] <= pivot) {
begin++;
}
arr[end] = arr[begin];
}
arr[begin] = pivot;
return begin;
}
public static void main(String[] args) {
int[] arr = {8, 5, 7, 6, 2, 1, 3};
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + " ");
}
}
}