forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicksort2_Sorting.java
More file actions
57 lines (52 loc) · 1.37 KB
/
Quicksort2_Sorting.java
File metadata and controls
57 lines (52 loc) · 1.37 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
48
49
50
51
52
53
54
55
56
57
package Sorting;
import java.util.*;
import java.io.*;
/*
* @author -- rajatgoyal715
*/
public class Quicksort2_Sorting {
public static void main(String[] args) throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
String s[]=br.readLine().split(" ");
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(s[i]);
partition(a,n);
}
public static String partition(int a[],int l){
if(l==0)
return "";
if(l==1)
return (a[0]+" ");
int temp=a[0];
int aleft[]=new int[l];
int amid[]=new int[l];
int aright[]=new int[l];
int left=0;
int mid=0;
int right=0;
String s1="";
String s2="";
String s3="";
String t;
for(int i=0;i<l;i++){
t=a[i]+" ";
if(a[i]<temp){
s1+=t;
aleft[left++]=a[i];
}
else if(a[i]==temp){
s2+=t;
amid[mid++]=a[i];
}
else{
s3+=t;
aright[right++]=a[i];
}
}
String part=partition(aleft,left)+s2+partition(aright,right);
System.out.println(part);
return part;
}
}