-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathQuickSort implementation.java
More file actions
46 lines (37 loc) · 964 Bytes
/
QuickSort implementation.java
File metadata and controls
46 lines (37 loc) · 964 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
//run on https://ide.geeksforgeeks.org/
import java.io.*;
class GFG {
public static int[] quickSort(int a[],int p,int r){
int q;
if(p<r){
q=partition(a,p,r);
quickSort(a,p,q-1);
quickSort(a,q+1,r);
}
return a;
}
public static int partition(int a[],int p,int r){
int x=a[r];
int i=p-1,j;
int temp;
for(j=p;j<=r-1;j++){
if(a[j]<=x){//i want the lesser elements(<=x) to be on left side and on right higher elements
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return i+1;
}
public static void main (String[] args) {
int a[]={9,16,18,5,11};
int b[]=quickSort(a,0,a.length-1);
for(int i=0;i<b.length;i++){
System.out.println(b[i]);
}
}
}