-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
48 lines (38 loc) · 1.07 KB
/
QuickSort.java
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
import java.util.Arrays;
class QuickSort{
public static void main(String[] args) {
int[] input = new int[]{1, 1, 0, 3};
int[] sorted = quickSort(input, 0, input.length - 1);
System.out.println(Arrays.toString(sorted));
}
private static int[] quickSort(int[] input, int left, int right) {
int mid = (left + right) / 2;
int i = left;
int j = right;
if((right - left) < 1) {
return input;
}
while(i <= j) {
while((input[i] <= input[mid]) && (i < mid)) {
i++;
}
while((input[j] > input[mid])) {
j--;
}
if(i <= j) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
i++;
j--;
}
}
if(j > left) {
input = quickSort(input, left, j);
}
if(i < right) {
input = quickSort(input, i, right);
}
return input;
}
}