-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort
57 lines (47 loc) · 1.42 KB
/
QuickSort
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
/* IMPORTANT: class must not be public. */
/*
* uncomment this if you want to read input.
import java.io.BufferedReader;
import java.io.InputStreamReader;
*/
class TestClass {
public static void main(String args[] ) throws Exception {
int a[]={2,4,1,6,8,5,3,7};
System.out.println("Starting quick sort");
// new TestClass().mergeSort(a);
// new TestClass().displayArray(a);
new TestClass().quickSort(a,0,a.length-1);
new TestClass().displayArray(a);
}
public void quickSort(int a[],int start,int end){
if (start<end){
int pIndex=partition(a,start,end);
System.out.println("PINDEX"+pIndex);
quickSort(a,start,pIndex-1);
quickSort(a,pIndex+1,end);
}
}
public int partition(int a[],int start,int end){
int pIndex=start;
for (int i=start;i<end;i++){
if(a[i]<=a[end]){
int temp=a[i];
a[i]=a[pIndex];
a[pIndex]=temp;
pIndex++;
}
}
int temp=a[end];
a[end]=a[pIndex];
a[pIndex]=temp;
displayArray(a);
// System.out.println("Starting quick sort pIndex"+pIndex);
return pIndex;
}
public void displayArray(int x[]){
for(int i:x){
System.out.print(i+" ");
}
System.out.println(" ");
}
}