-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
57 lines (45 loc) · 1.34 KB
/
QuickSort.java
File metadata and controls
57 lines (45 loc) · 1.34 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
import java.util.Arrays;
import java.util.Random;
public class QuickSort {
public void quickSort(long arr[], int low, int high) {
if (low < high) {
int pidx = partition(arr, low, high);
quickSort(arr, low, pidx - 1);
quickSort(arr, pidx+1, high);
}
}
public static int partition(long arr[], int low, int high) {
long pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}
public static void swap(long arr[],int low ,int high){
long temp = arr[low];
arr[low ] = arr[high];
arr[high] = temp;
}
public static void main(String[] args) {
Random rand = new Random();
long[] arr = new long[100000];
for (int i = 0; i < arr.length; i++) {
arr[i] = rand.nextInt(1000);
System.out.print(arr[i]+" ");
}
QuickSort qs = new QuickSort();
long start = System.nanoTime();
qs.quickSort(arr, 0, arr.length - 1);
long end = System.nanoTime();
long total = end - start;
double elapsedTime = (double) total / 1_000_000_000;
System.out.println("\nSorted array is: ");
System.out.println(Arrays.toString(arr));
System.out.println("Time taken to sort "+arr.length+" is "+ elapsedTime +" seconds");
}
}