-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.java
More file actions
46 lines (38 loc) · 1.13 KB
/
Copy pathquick_sort.java
File metadata and controls
46 lines (38 loc) · 1.13 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
public class quick_sort {
static int partition(int[] arr, int lb, int ub) {
int pivot = arr[lb];
int start = lb;
int end = ub;
while (start < end) {
while (start < arr.length && arr[start] <= pivot) {
start++;
}
while (arr[end] > pivot) {
end--;
}
if (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
int temp = arr[lb];
arr[lb] = arr[end];
arr[end] = temp;
return end;
}
static void quickSort(int[] arr, int lb, int ub) {
if (lb < ub) {
int loc = partition(arr, lb, ub);
quickSort(arr, lb, loc - 1);
quickSort(arr, loc + 1, ub);
}
}
public static void main(String[] args) {
int[] arr = {7, 6, 10, 5, 9, 2, 1, 15, 7};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}