-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuickSort.java
More file actions
47 lines (39 loc) · 1.09 KB
/
QuickSort.java
File metadata and controls
47 lines (39 loc) · 1.09 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
package algorithm.sort;
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
// unsorted input array
int[] arr = { 5, 2, 1, 0, 34, 88, 12, 3, 8, 33, 76 };
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
private static void quickSort(int[] arr, int low, int high) {
if (low >= high)
return;
// find pivot
int pivotIndex = findPivotIndex(arr, low, high);
// sort the left half
quickSort(arr, low, pivotIndex - 1);
// sort the right half
quickSort(arr, pivotIndex + 1, high);
}
private static int findPivotIndex(int[] arr, int low, int high) {
// randomly find the pivot element
int pivotElement = arr[high];
// move elements smaller to pivot to its left and larger to its right
int j = low;
for (int i = low; i < high; i++) {
if (arr[i] <= pivotElement) {
swap(arr, i, j);
j++;
}
}
swap(arr, j, high);
return j;
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}