forked from keshavagarwal17/All-Cp-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
83 lines (73 loc) · 2.47 KB
/
QuickSort.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.*;
public class QuickSort{
static int divide(int a[], int low, int high)
{
int p = a[high];
int i = (low-1);
for (int j=low; j<high; j++)
{
if (a[j] < p)
{
i++;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
int temp = a[i+1];
a[i+1] = a[high];
a[high] = temp;
return i+1;
}
static void quickSort(int a[], int low, int high)
{
if (low < high)
{
int q = divide(a, low, high);
quickSort(a, low, q-1);
quickSort(a, q+1, high);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int n;
System.out.print("Enter the size of the array : ");
n = scan.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[n];
int r = 3*n;
for(int i=0;i<n;i++)
{
a[i] = i;
b[i] = n-i;
c[i] = rand.nextInt(r);
}
int[] a1 = new int [n];
int[] b1 = new int [n];
int[] c1 = new int [n];
for(int j = 0 ; j < 15 ; j++)
{
for(int i = 0 ; i < n ; i++)
{
a1[i] = a[i];
b1[i] = b[i];
c1[i] = c[i];
}
final long startTimeA = System.nanoTime();
quickSort(a1,0,n-1);
final long durationA = (System.nanoTime() - startTimeA);
final long startTimeB = System.nanoTime();
quickSort(b1,0,n-1);
final long durationB = (System.nanoTime() - startTimeB);
final long startTimeC = System.nanoTime();
quickSort(c1,0,n-1);
final long durationC = (System.nanoTime() - startTimeC);
System.out.println("The time required for quick sort for a[] is : " + durationA + " nanoseconds.");
System.out.println("The time required for quick sort for b[] is : " + durationB + " nanoseconds.");
System.out.println("The time required for quick sort for c[] is : " + durationC + " nanoseconds.");
System.out.println(" ");
}
}
}