File tree 1 file changed +48
-0
lines changed 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ public class Quick {
3
+ static int count = 0 ;
4
+ public static void main (String arg []){
5
+ Scanner sc = new Scanner (System .in );
6
+ System .out .println ("enter size of array:" );
7
+ int n =sc .nextInt ();
8
+ int []a =new int [n ];
9
+ for (int i =0 ;i <n ;i ++){
10
+ a [i ]=(int )(Math .random ()*100 );
11
+ }
12
+ long s =System .nanoTime ();
13
+ Quick ob = new Quick ();
14
+ ob .quick (a ,0 ,a .length - 1 );
15
+ long e =System .nanoTime ();
16
+ System .out .println ("total time is: " +(e -s ));
17
+ System .out .println ("sorted array is:" );
18
+ for (int i =0 ;i <n ;i ++){
19
+ System .out .println ("" +a [i ]);
20
+ }
21
+ System .out .println ("number of partition call is:" +count );
22
+ }
23
+ void quick (int a [],int p ,int r ){
24
+ if (p < r ){
25
+ int q = part (a ,p ,r );
26
+ count ++;
27
+ quick (a , p , q -1 );
28
+ quick (a , q +1 , r );
29
+ }
30
+ }
31
+ int part (int a [],int p ,int r ){
32
+ int pivot =a [r ];
33
+ int i =p -1 ;
34
+ for (int j =p ;j <=r -1 ;j ++){
35
+ if (a [j ]<=pivot ){
36
+ i ++;
37
+ int temp =a [i ];
38
+ a [i ]=a [j ];
39
+ a [j ]=temp ;
40
+ }
41
+ }
42
+ int temp =a [i +1 ];
43
+ a [i +1 ]=a [r ];
44
+ a [r ]=temp ;
45
+ return (i +1 );
46
+ }
47
+
48
+ }
You can’t perform that action at this time.
0 commit comments