generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.java
41 lines (37 loc) · 1.08 KB
/
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
37
38
39
40
41
public class quicksort {
public static int partition(int arr[], int lb, int ub) {
int pivot = arr[lb]; // Choose the first element as the pivot
int start = lb;
int end = ub;
while (start < end) {
while (arr[start] <= pivot && start < end) {
start++;
}
while (arr[end] > pivot) {
end--;
}
if (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
arr[lb] = arr[end];
arr[end] = pivot;
return end;
}
public static void quick_sort(int arr[], int lb, int ub) {
if (lb < ub) {
int loc = partition(arr, lb, ub);
quick_sort(arr, lb, loc - 1);
quick_sort(arr, loc + 1, ub);
}
}
public static void main(String args[]) {
int arr[] = { 3, 2, 1, 4, 7, 6 };
quick_sort(arr, 0, 5);
for (int i = 0; i < 6; i++) {
System.out.println(arr[i]);
}
}
}