-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathQuickSort.java
More file actions
48 lines (44 loc) · 870 Bytes
/
QuickSort.java
File metadata and controls
48 lines (44 loc) · 870 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
43
44
45
46
47
48
class QuickSort {
public static void main(String[]args) {
int a[]={10,75,-15,17,0,-2};
QSort(a,0,a.length-1);
System.out.println("Sorted Array : ");
System.out.print("[");
for(int i=0;i<a.length;i++) {
if(i == a.length-1)
System.out.print(a[i]);
else
System.out.print(a[i] + ", ")
}
System.out.println("]");
}
public static void QSort(int a[],int l,int h) {
if(l<h) {
int loc=Partion(a,l,h);
QSort(a,l,loc-1);
QSort(a,loc+1,h);
}
}
public static int Partion(int a[],int l,int h) {
int pivot = a[l];
int start=l;
int end=h;int tmp;
while(start < end) {
while(start<=h && a[start]<=pivot) {
start++;
}
while(a[end]>pivot) {
end--;
}
if(start<end) {
tmp=a[start];
a[start]=a[end];
a[end]=tmp;
}
}
tmp=a[l];
a[l]=a[end];
a[end]=tmp;
return end;
}
}