-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathQuickSort.java
More file actions
52 lines (37 loc) · 1.05 KB
/
Copy pathQuickSort.java
File metadata and controls
52 lines (37 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
49
50
51
52
package util;
/**
* Created by nikoo28 on 10/11/20 4:53 PM
*/
public class QuickSort {
public static void quickSort(int[] arr, int begin, int end) {
if (begin < end) {
// Find the partition
int partition = findPartition(arr, begin, end);
// Do quick sort on the left part
quickSort(arr, begin, partition - 1);
// Do quick sort on the right part
quickSort(arr, partition + 1, end);
}
}
private static int findPartition(int[] arr, int begin, int end) {
// Taking last element as pivot element
int pivot = arr[end];
int i = (begin - 1); // index of smaller element
for (int j = begin; j < end; j++) {
// If current element is smaller than the pivot
if (arr[j] < pivot) {
i++;
// swap arr[i] and arr[j]
swap(arr, i, j);
}
}
// swap arr[i+1] and arr[high] (or pivot)
swap(arr, i + 1, end);
return i + 1;
}
private static void swap(int[] arr, int i, int j) {
int swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
}
}