forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick.java
More file actions
48 lines (47 loc) · 1.51 KB
/
Quick.java
File metadata and controls
48 lines (47 loc) · 1.51 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
import java.util.*;
public class Quick {
static int count = 0;
public static void main(String arg[]){
Scanner sc = new Scanner(System.in);
System.out.println("enter size of array:");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++){
a[i]=(int)(Math.random()*100);
}
long s =System.nanoTime();
Quick ob = new Quick();
ob.quick(a,0,a.length - 1);
long e=System.nanoTime();
System.out.println("total time is: "+(e-s));
System.out.println("sorted array is:");
for(int i=0;i<n;i++){
System.out.println(""+a[i]);
}
System.out.println("number of partition call is:"+count);
}
void quick(int a[],int p,int r){
if(p < r){
int q = part(a,p,r);
count++;
quick(a, p, q-1);
quick(a, q+1, r);
}
}
int part(int a[],int p,int r){
int pivot=a[r];
int i=p-1;
for(int j=p;j<=r-1;j++){
if(a[j]<=pivot){
i++;
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return (i+1);
}
}