-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickSort.java
103 lines (84 loc) · 2.53 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
/*
* Brett Waugh
* 3 December 2019
* quickSort.java
* The program is the logic for the Quicksort
* algorithms. There are four different
* Quicksort implementations for this project.
* All of the Quicksort implementations use
* recursion and arrays.
*/
public class quickSort {
// Displays the contents of an array.
// Used in troubleshooting but not in final program.
public static void display(int max, int[] data) {
for (int i = 0; i < max; i++) {
System.out.print(data[i] + ", ");
}
System.out.println("\n");
}
// The call to the recursive quicksort function.
public static void quickSortCall(int max, int[] data, int partSize) {
recurQuickSort(0, max - 1, data, partSize);
}
// The recursive quicksort function.
// Ended up including an additional argument to do the median-of-three
// part instead of creating a separate function.
public static void recurQuickSort(int left, int right, int[] data, int partSize) {
if (left >= right) {
return;
}
int median = medOf3(left, right, data);
int partition = partitionItMed3(left, right, median, data);
recurQuickSort(left, partition - 1, data, partSize);
recurQuickSort(partition + 1, right, data, partSize);
}
// Similar to the above function with some slight differences
// to the pointers. For use with the median-of-three case.
public static int partitionItMed3(int left, int right, int pivot, int[] data) {
int lpoint = left - 1;
int rpoint = right;
while (lpoint < rpoint) {
while (data[++lpoint] < pivot)
;
// Search for a smaller entry.
while (rpoint > 0 && data[--rpoint] > pivot)
;
// If the pointers run into each other, swapping stops.
if (lpoint >= rpoint) {
break;
} else {
swap(lpoint, rpoint, data);
}
}
swap(lpoint, right, data);
// Returns the location of the pivot.
return lpoint;
}
// Swaps two elements in the array.
public static void swap(int index1, int index2, int[] data) {
int temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
// Median-of-three function.
public static int medOf3(int left, int right, int[] data) {
int cent = (left + right) / 2;
// Case for left and center.
if (data[left] > data[cent]) {
swap(left, cent, data);
}
// Case for left and right.
if (data[left] > data[right]) {
swap(left, right, data);
}
// Case for center and right.
if (data[cent] > data[right]) {
swap(cent, right, data);
}
// Puts pivot value to the right.
swap(cent, right, data);
// Returns the median value.
return data[right];
}
}