-
Notifications
You must be signed in to change notification settings - Fork 24
/
quick_sort.java
40 lines (38 loc) · 1 KB
/
quick_sort.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
import java.util.Scanner;
public class quick_sort {
public static int part(int []a,int s,int e){
int pivot=a[e];
int i=s;
for(int j=s;s<e;j++){
if(a[j]<=pivot){
int temp=a[j];
a[j]=a[i];
i++;
}
}
int temp=a[i];
a[i]=a[e];
a[e]=temp;
return i;
}
public static void sort(int []a,int s,int e){
if(s<e){
int parti=part(a,s,e);
sort(a,s,parti-1);
sort(a,parti+1,e);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int []a=new int[n]; // 2 9 3 7 1 8
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
sort(a,0,n);
for(int i=0;i<n;i++)
System.out.println(a[i]);
long s=System.nanoTime();
long e=System.nanoTime();
System.out.println(e-s);
}
}