forked from arkaanfast/DAA-LAB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick.java
More file actions
59 lines (58 loc) · 1.32 KB
/
Quick.java
File metadata and controls
59 lines (58 loc) · 1.32 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
58
59
import java.util.Random;
import java.util.Scanner;
public class Quick {
public static void sort(int a[]) {
quicksort(a, 0, a.length - 1);
}
public static void quicksort(int a[], int low, int high) {
int j;
if(low <= high) {
j = partition(a, low, high);
quicksort(a, low, j-1);
quicksort(a, j+1, high);
}
}
public static int partition(int a[], int low, int high) {
int i = low + 1;
int j = high;
int pivot = a[low];
while(true) {
while(i < j && pivot > a[i])
i++;
while(pivot < a[j])
j--;
if(i < j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else {
int temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n;
System.out.println("enter the size of the array");
n = in.nextInt();
int a[] = new int[n];
Random random = new Random();
System.out.println("the randomly generated array is");
for(int i = 0;i < n;i++) {
a[i] = Math.abs(random.nextInt(20));
System.out.print(a[i] + " ");
}
System.out.println();
long startTime = System.nanoTime();
sort(a);
long stopTime = System.nanoTime();
long elapsedTime = stopTime - startTime;
System.out.println("the sorted array is ");
for(int i = 0;i < n;i++)
System.out.print(a[i] + " ");
}
}