-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject6.java
More file actions
100 lines (79 loc) · 1.96 KB
/
Project6.java
File metadata and controls
100 lines (79 loc) · 1.96 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
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
package project6;
public class Project6 {
public static void main(String[] args) {
int[] testArray = {4,27,35,3, 8, 1, 3, 87, 98, 2};
System.out.print("Intial Print: ");
print(testArray, testArray.length);
quicksort(testArray);
System.out.print("QuickSort Test Print: ");
print(testArray, testArray.length);
quicksortHoarePartition(testArray);
System.out.print("Hoare QuickSort Test Print: ");
print(testArray, testArray.length);
}
public static void quicksort(int[] a ) {
int high = a.length - 1;
Swap(a, 0, high);
partitionQS(a, 0, high);
quickSort(a, 0, high);
}
public static void quicksortHoarePartition(int[] a) {
int high = a.length - 1;
partitionH(a, 0, high);
sortHP(a, 0, high);
}
public static void print(int[] a, int length) {
for (int i = 0; i < length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
//make two classes separately, Hoare and regular partion
static void Swap(int[] a, int num1, int num2){
int temp = a[num1];
a[num1] = a[num2];
a[num2] = temp;
}
static int partitionQS(int []a, int low, int high){
int pivot = a[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++){
if (a[j] <= pivot){
i++;
Swap(a, i, j);
}
}
Swap(a, i + 1, high);
return (i + 1);
}
static void quickSort(int[] a, int low, int high){
if (low < high) {
int partIndex = partitionQS(a, low, high);
quickSort(a, low, partIndex - 1);
quickSort(a, partIndex + 1, high);
}
}
static int partitionH(int[] a, int low, int high) {
int pivot = a[low];
int i = low - 1;
int j = high + 1;
while(true) {
do { i++;
}while (a[i]< pivot);
do { j--;
}while (a[j] > pivot);
if(i >= j)
return j;
int hold = a[i];
a[i] = a[j];
a[j] = hold;
}
}
static void sortHP (int[]a , int low, int high) {
if (low < high){
int partIndex = partitionH( a, low, high);
sortHP(a, low, partIndex - 1 );
sortHP(a, partIndex + 1, high);
}
}
}