forked from ool2o8/tech-interview-for-developer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
42 lines (26 loc) · 718 Bytes
/
Copy pathQuickSort.java
File metadata and controls
42 lines (26 loc) · 718 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
import java.util.Arrays;
public class QuickSort {
static int[] arr = {5, 1, 1, 2, 1, 4, 4, 4, 5, 5};
public static void main(String[] args) throws Exception {
quickSort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int[] arr, int start, int end) {
if(start >= end) return;
if(start < end) {
int i = start-1;
int j = end+1;
int pivot = arr[(start+end)/2];
while(i < j) {
while(arr[++i] < pivot) {}
while(arr[--j] > pivot) {}
if (i >= j) break;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
quickSort(arr, start, i-1);
quickSort(arr, j+1, end);
}
}
}