-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick.java
More file actions
48 lines (39 loc) · 1.05 KB
/
Quick.java
File metadata and controls
48 lines (39 loc) · 1.05 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
public class Quick {
public static void main(String[] args) {
int[] arr = { 456, 3, 64, 23, 67, 9, 34 };
printArray(arr);
quicksort(arr, 0, arr.length - 1);
printArray(arr);
}
static void quicksort(int arr[], int l, int u) {
if (l < u) {
int p = partition(arr, l, u);
quicksort(arr, l, p - 1);
quicksort(arr, p + 1, u);
}
}
private static int partition(int[] arr, int l, int u) {
int pivot = l;
int lp = l + 1;
int rp = u;
while (lp < rp) {
while (arr[lp] < pivot)
lp++;
while (arr[rp] > pivot)
rp--;
int temp = arr[lp];
arr[lp] = arr[rp];
arr[rp] = temp;
}
int temp = arr[pivot];
arr[pivot] = arr[lp];
arr[lp] = temp;
return lp;
}
private static void printArray(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println();
}
}