-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortTester.java
More file actions
217 lines (180 loc) · 5.69 KB
/
QuickSortTester.java
File metadata and controls
217 lines (180 loc) · 5.69 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//Team SoSoSlow -- Herman Lin, Elaina Chung, Lisa Eng
//APCS2 pd3
//LAB01 -- What Does the Data Say?
//2017-03-09
/*
PIVOT SELECTION AFFECT EXECUTION TIME
Create an array (arrayOne) that partitions with different pivots.
BEST CASE PIVOT: median of the array
WORST CASE PIVOT: smallest/largest value of the array
AVERAGE CASE PIVOT: val at the middle index of arary
After each run, using System.nanoTime() to time start and end time to find runtime
DATA ARRANGEMENT AFFECT EXECUTION TIME
Using the average case pivot to partition, we will find execution time for the following arrays:
SHUFFLE
After each run, using System.nanoTime() to time start and end time to find runtime
*/
public class QuickSortTester {
public static void swap( int x, int y, int[] o ) {
int tmp = o[x];
o[x] = o[y];
o[y] = tmp;
}
//print input array
public static void printArr( int[] a ) {
for ( int o : a )
System.out.print( o + " " );
System.out.println();
}
//shuffle elements of input array
public static void shuffle( int[] d ) {
int tmp;
int swapPos;
for( int i = 0; i < d.length; i++ ) {
tmp = d[i];
swapPos = i + (int)( (d.length - i) * Math.random() );
swap( i, swapPos, d );
}
}
//================================================
// TEST PIVOT BEST CASE
public static void qsort( int[] d ) {
quicksort (d, 0, d.length - 1);
}
public static void quicksort (int[] arr, int left, int right) {
if (left < right) {
int pvtPos = partition (arr, left, right, 0);
quicksort (arr, left, pvtPos - 1);
quicksort (arr, pvtPos + 1, right);
}
}
public static int partition(int arr[], int left, int right, int pvtPos) {
int pvtVal = arr[pvtPos];
swap(pvtPos, right, arr);
int storVal = left;
for (int i = left; i < right; i++) {
if (arr[i] <= pvtVal) {
swap(i, storVal, arr);
storVal++;
}
}
swap(storVal, right, arr);
return storVal;
}
//==================================================
//TEST PIVOT WORST CASE
public static int findMinPos (int[] a){
int min = a[0];
int minPos = 0;
for (int x = 0; x < a.length; x+=1){
if (a[x] < min){
min = a[x];
minPos = x;
}
}
return minPos;
}
public static int findMaxPos (int[] a){
int max = a[0];
int maxPos = 0;
for (int x = 0; x < a.length; x+=1){
if (a[x] > max){
max = a[x];
maxPos = x;
}
}
return maxPos;
}
public static void qsortWC( int[] d, int minMaxPos ) {
quicksortWC (d, 0, d.length - 1, minMaxPos);
}
public static void quicksortWC (int[] arr, int left, int right, int minMaxPos) {
if (left < right) {
int pvtPos = partition(arr, left, right, minMaxPos);
quicksortWC (arr, left, pvtPos - 1, minMaxPos);
quicksortWC (arr, pvtPos + 1, right, minMaxPos);
}
}
public static void main (String[] args) {
//best: pvtPos = median
//worst: pvtPos = biggest/smallest value
//average: pvtPos = middle element
//BEST CASE
int[] arrayOne = {1, 2, 3, 4, 5};
int[] arrayTwo = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arrayThree = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
System.out.println("Checking case for:");
printArr(arrayOne);
double bestStartTime = 0;
double bestEndTime = 0;
double bestTime = 0;
double bestSummation = 0;
for (int x = 0; x < 10; x ++) {
bestStartTime = System.nanoTime();
qsort(arrayOne);
bestEndTime = System.nanoTime();
bestTime = (bestEndTime - bestStartTime);
System.out.println("Test " + x + ": " + bestTime + " nanoseconds");
bestSummation += bestTime;
}
System.out.println("\nAverage Time: " + bestSummation/10 + " nanoseconds");
//WORST CASE
System.out.println("Checking case for:");
printArr(arrayOne);
int minPos = findMinPos(arrayOne);
int maxPos = findMaxPos(arrayOne);
double worstStartTime = 0;
double worstEndTime = 0;
double worstTime = 0;
double worstSummation = 0;
for (int x = 0; x < 10; x ++) {
worstStartTime = System.nanoTime();
qsortWC(arrayOne, maxPos);
worstEndTime = System.nanoTime();
worstTime = (worstEndTime - worstStartTime);
System.out.println("Test " + x + ": " + worstTime + " nanoseconds");
worstSummation += worstTime;
}
System.out.println("\nAverage Time: " + worstSummation/10 + " nanoseconds");
//=-=-=-=-=-=AVERAGE CASE TESTING=-=-=-=-=-=
System.out.println("Checking case for:");
printArr(arrayTwo);
double avgStartTime = 0;
double avgEndTime = 0;
double avgTime = 0;
double avgSummation = 0;
for (int x = 40; x < 60; x ++) {
for (int y = 0; y < 5; y ++) {
avgStartTime = System.nanoTime();
qsortWC(arrayTwo, x);
avgEndTime = System.nanoTime();
avgTime = avgEndTime - avgStartTime;
//System.out.println("Test " + y + ": " + avgTime + " nanoseconds");
avgSummation += avgTime;
}
System.out.println("Average Time for index " + x + ": " + avgSummation/5 + " nanoseconds");
avgSummation = 0;
}
shuffle(arrayTwo);
System.out.println("\nChecking case for:");
printArr(arrayTwo);
double avgShuffleST = 0;
double avgShuffleET = 0;
double avgShuffleT = 0;
double avgShuffleS = 0;
for (int x = 40; x < 60; x ++) {
for (int y = 0; y < 5; y ++) {
avgShuffleST = System.nanoTime();
qsortWC(arrayTwo, x);
avgShuffleET = System.nanoTime();
avgShuffleT = avgShuffleET - avgShuffleST;
//System.out.println("Test " + y + ": " + avgShuffleT + " nanoseconds");
avgShuffleS += avgShuffleT;
}
System.out.println("Average Time for index " + x + ": " + avgShuffleS/5 + " nanoseconds");
avgShuffleS = 0;
}
/*=====================================================================
=====================================================================*/
}
}