-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
QuicksortIn-Place.java
42 lines (40 loc) · 1023 Bytes
/
QuicksortIn-Place.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
import java.io.*;
import java.util.*;
public class Solution {
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);
}
}