forked from shivangdubey/HacktoberFest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
55 lines (39 loc) · 822 Bytes
/
QuickSort.java
File metadata and controls
55 lines (39 loc) · 822 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
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
public static void main(String[] args) {
int[] arr = { 10, 20, 30, 40, 10 };
quickSort(arr, 0, arr.length - 1);
for (int val : arr) {
System.out.print(val + " ");
}
}
public static void quickSort(int[] arr, int lo, int hi) {
if (lo >= hi) {
return;
}
int mid = (lo + hi) / 2;
int pivot = arr[mid];
// partitioning
int left = lo;
int right = hi;
while (left <= right) {
// left problem
while (arr[left] < pivot) {
left++;
}
// right problem
while (arr[right] > pivot) {
right--;
}
// problem solve
if (left <= right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
// partitioning completed
// smaller parts sort
quickSort(arr, lo, right);
quickSort(arr, left, hi);
}