-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQuickSortSoln.java
61 lines (46 loc) · 995 Bytes
/
QuickSortSoln.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
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Arrays;
public class QuickSortSoln {
private static int partititon(int[] a, int si, int ei) {
int pivotElement=a[si];
int smallerNumCount=0;
for(int i=si+1; i<=ei; i++) {
if(a[i]<pivotElement) {
smallerNumCount++;
}
}
int pivotIndex=smallerNumCount+si;
a[si]=a[smallerNumCount+si];
a[smallerNumCount+si]=pivotElement;
int i=si;
int j=ei;
while(i<j) {
if(a[i]<pivotElement) {
i++;
}
else if(a[j]>=pivotElement) {
j--;
}
else {
int temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
}
return pivotIndex;
}
public static void quickSort(int[] a,int si,int ei) {
if(si>=ei) {
return ;
}
int pivotIndex=partititon(a,si,ei);
quickSort(a, si, pivotIndex-1);
quickSort(a, pivotIndex+1, ei);
}
public static void main(String[] args) {
int[] a= {10,4,5,9,8,6,12,11,7,888,777,111};
quickSort(a, 0, a.length-1);
System.out.println(Arrays.toString(a));
}
}