-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.java
More file actions
66 lines (46 loc) · 1.3 KB
/
quickSort.java
File metadata and controls
66 lines (46 loc) · 1.3 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package DSAsorting.sorting;
public class quickSort {
static void swap(int arr[], int x, int y) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
static int partition(int arr[], int st, int end) {
int pivot = arr[st];
int cnt = 0;
for (int i = st + 1; i <= end; i++) {
if (arr[i] <= pivot)
cnt++;
}
int PI = st + cnt;
swap(arr, st, PI);
int i = st, j = end;
while (i < PI && j > PI) {
while (arr[i] <= pivot)
i++;
while (arr[j] > pivot)
j--;
if (i < PI && j > PI) {
swap(arr, i, j);
i++;
j--;
}
}
return PI;
}
static void quicks(int arr[], int st, int end) {
if (st >= end)
return;
int piv = partition(arr, st, end);
quicks(arr, st, piv - 1);
quicks(arr, piv + 1, end);
}
public static void main(String arrgs[]) {
int arr[] = { 2, 5, 8, 6, 4, 5, 5, 6 };
int n = arr.length;
quicks(arr, 0, n - 1);
for (int k : arr) {
System.out.print(k + " ");
}
}
}