forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicksortInPlace.java
More file actions
47 lines (44 loc) · 1.06 KB
/
QuicksortInPlace.java
File metadata and controls
47 lines (44 loc) · 1.06 KB
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
package Sorting;
import java.util.*;
import java.io.*;
/*
* @author -- rajatgoyal715
*/
public class QuicksortInPlace {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String s[]=br.readLine().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(s[i]);
sort(a,0,n-1);
}
public static void sort(int a[],int s,int e){
if(s>=e)
return;
if(e-s+1<=0)
return;
int pivot=a[e];
int i=s;
int m;
for(int j=s;j<e;j++){
if(a[j]<=pivot){
m=a[j];
a[j]=a[i];
a[i]=m;
i++;
}
}
m=a[e];
a[e]=a[i];
a[i]=m;
String arr="";
for(int k=0;k<a.length;k++){
arr+=a[k]+" ";
}
System.out.println(arr);
sort(a,s,i-1);
sort(a,i+1,e);
}
}