-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick_Sort.java
77 lines (62 loc) · 2.37 KB
/
Quick_Sort.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
// this is based upon the tutorial made by geeksforgeeks.org
// https://www.geeksforgeeks.org/quick-sort-algorithm/
import javax.swing.JOptionPane;
public class Quick_Sort {
public static void main(){
// get the array size from the user
int size = 0;
while(size <= 0){
size = Integer.parseInt(JOptionPane.showInputDialog("How big is the array you want to sort?"));
if(size <= 0){
JOptionPane.showMessageDialog(null, "Array size must be greater than 0");
}
}
// create a new array and populate each element with the users input
double[] array = new double[size];
for(int i = 0;i < size; i++){
array[i] = Double.parseDouble(JOptionPane.showInputDialog("Please enter element " + (i + 1) + " for this array"));
}
// output the original array
System.out.print("\n Original Array: " + array[0]);
for(int i = 1; i < size; i++){
System.out.print(", " + array[i]);
}
// sort the array
quickSort(array, 0, array.length - 1);
// output the sorted array
System.out.print("\n Sorted Array: " + array[0]);
for(int i = 1; i < size; i++){
System.out.print(", " + array[i]);
}
}
private static int count = 0;
public static void quickSort(double[] array, int low, int high) {
if (low < high) {
int pivotIndex = partition(array, low, high);
count++;
System.out.print("\n Iteration " + count + ": " + array[0]);
for(int i = 1; i < array.length; i++){
System.out.print(", " + array[i]);
}
quickSort(array, low, pivotIndex - 1);
quickSort(array, pivotIndex + 1, high);
}
}
private static int partition(double[] array, int low, int high) {
double pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(array, i, j);
}
}
swap(array, i + 1, high);
return i + 1;
}
private static void swap(double[] array, int i, int j) {
double temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}