-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathQuickSort.java
More file actions
30 lines (27 loc) · 806 Bytes
/
QuickSort.java
File metadata and controls
30 lines (27 loc) · 806 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
public class QuickSort {
public static void quickSort(int[] num, int n){
quickSortRecursive(num, 0, n - 1);
}
public static void quickSortRecursive(int[] num, int l, int r){
if(l >= r) return;
int div = partition(num, l, r);
quickSortRecursive(num, l, div - 1);
quickSortRecursive(num, div + 1, r);
}
public static int partition(int[] num, int l, int r){
int pivot = num[r];
int index = l;
for(int i = l; i < r; i++){
if(num[i] > pivot){
int temp = num[index];
num[index] = num[i];
num[i] = temp;
index++;
}
}
int temp = num[index];
num[index] = num[r];
num[r] = temp;
return index;
}
}