-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
145 lines (124 loc) · 3.24 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Author: Alara Dasdan
// Date: Jul 2020
package Sorting;
import java.util.Random;
// Sorting numbers using the quicksort algorithm
public class QuickSort {
static int seed = 12345; // random number generator seed
static int sz = 10; // size of the array
// generate a random num in a given range
public static double randDouble(Random rd, double min, double max) {
return min + rd.nextDouble() * (max - min + 1);
}
// generate array of random numbers of size sz
public static double[] getRandNumbers(int sz) {
Random rd = new Random(seed);
double[] arr = new double[sz]; // array of random numbers
// generate random numbers
for (int i = 0; i < arr.length; i++) {
double r = (int) (10 * randDouble(rd, 0, 10));
arr[i] = r;
}
return arr;
}
// print input array arr in given order; also verify if sorted
public static void printNums(double[] arr, boolean isAscending) {
System.out.format("\nprint array of size %d:\n", arr.length);
String sep = "";
for (int i = 0; i < arr.length; i++) {
System.out.format(sep + "%.2f", arr[i]);
sep = ", ";
}
System.out.println();
boolean isSorted = checkSort(arr, isAscending);
System.out.println("is sorted? " + isSorted);
}
// swap arr[x] and arr[y]
private static void swap(double[] arr, int x, int y) {
double tmp = arr[x];
arr[x] = arr[y];
arr[y] = tmp;
}
// sort input array arr[l..r] using quicksort; partition is embedded in
public static void sortRecur(double[] arr, int l, int r, boolean isAscending) {
if (l >= r) {
return;
}
int p = l;
int li = l;
int ri = r;
if (isAscending) {
while (li <= ri) {
if (arr[li] <= arr[p]) {
li++;
}
if (arr[p] < arr[ri]) {
ri--;
}
if ((li < ri) && (arr[li] > arr[p]) && (arr[p] > arr[ri])) {
swap(arr, li, ri);
li++;
ri--;
}
}
if (ri > 0) {
swap(arr, p, ri);
}
} else {
while (li <= ri) {
if (arr[li] >= arr[p]) {
li++;
}
if (arr[p] > arr[ri]) {
ri--;
}
if ((li < ri) && (arr[li] < arr[p]) && (arr[p] < arr[ri])) {
swap(arr, li, ri);
li++;
ri--;
}
}
if (ri > 0) {
swap(arr, p, ri);
}
}
sortRecur(arr, l, ri - 1, isAscending);
sortRecur(arr, ri + 1, r, isAscending);
}
// sort input array into a new array and return it
public static double[] sort(double[] arr, boolean isAscending) {
double[] arr2 = arr;
sortRecur(arr2, 0, arr.length - 1, isAscending);
return arr2;
}
// verify if arr is sorted in the given order
public static boolean checkSort(double[] arr, boolean isAscending) {
for (int j = 0; j < (arr.length - 1); j++) {
if (isAscending) {
if (arr[j] > arr[j + 1]) {
return false;
}
} else {
if (arr[j] < arr[j + 1]) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
boolean isAscending = false;
double[] arr = getRandNumbers(sz);
printNums(arr, isAscending);
double[] arr2 = sort(arr, isAscending);
printNums(arr2, isAscending);
arr = getRandNumbers(0);
printNums(arr, isAscending);
arr2 = sort(arr, isAscending);
printNums(arr2, isAscending);
arr = getRandNumbers(1);
printNums(arr, isAscending);
arr2 = sort(arr, isAscending);
printNums(arr2, isAscending);
}
}