-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
179 lines (149 loc) · 4.77 KB
/
QuickSort.java
File metadata and controls
179 lines (149 loc) · 4.77 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
/*Herman Line
APCS2 - pd3
HW15 -- So So Quick
2017-3-8*/
/*****************************************************
* class QuickSort
*
* Implements quicksort algo to sort an array of ints in place
*
* 1. Summary of QuickSort algorithm:
* QSort(arr): divide the array in half and call quicksort
* on each half. Eqch call on quicksort calls partition
* to "sort" each of the halves.
*
* 2a. Worst pivot choice
* state: pivot choice at either ends of the array
* runtime: O(n^2)
*
* 2b. Best pivot choice / array state and associated runtime:
* state: pivot choice is in the center
* runtime: O(nlogn)
*
* 3. Approach to handling duplicate values in array:
* There is no specific way to handle duplicates because
* of the nature of the partition method. Partition moves
* values smaller than the value of the pivot position to
* the left and values larger than the value of the pivot
* position to the right. If there are duplicates (i.e 2, 2)
* it's only a matter of whether the computer recognizes
* 2 as greater than 2 or less than 2 (most likely the latter).
*****************************************************/
/***
PROTIP: Assume no duplicates during initial development phase.
Once you have a working implementation, test against arrays
with duplicate values, and revise if necessary. (Backup first.)
***/
public class QuickSort {
//--------------v HELPER METHODS v--------------
//swap values at indices x, y in array o
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 );
}
}
//return int array of size s, with each element fr range [0,maxVal)
public static int[] buildArray( int s, int maxVal ) {
int[] retArr = new int[s];
for( int i = 0; i < retArr.length; i++ )
retArr[i] = (int)( maxVal * Math.random() );
return retArr;
}
//--------------^ HELPER METHODS ^--------------
/*****************************************************
* void qsort(int[])
* @param d -- array of ints to be sorted in place
*
* quicksort (arr, left, right)
* if left < right
* pvtPos = partition (arr, left, right)
* quicksort (arr, left, pvtPos - 1)
* quicksort (arr, pvtPos + 1, right)
*****************************************************/
public static void qsort( int[] d ) {
quicksort (d, leftBound, rightBound);
}
// Thinkers are encouraged to roll their own subroutines.
// Insert your auxiliary helper methods here.
//quicksort method
public static void quicksort (int[] arr, int left, int right) {
if (left < right) {
int pvtPos = partition (arr, left, right, (left + right) / 2);
quicksort (arr, left, pvtPos - 1);
quicksort (arr, pvtPos + 1, right);
}
}
//partition method
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;
}
/*
//main method for testing
public static void main( String[] args ) {
//get-it-up-and-running, static test case:
int [] arr1 = {7,1,5,12,3};
System.out.println("\narr1 init'd to: " );
printArr(arr1);
qsort( arr1 );
System.out.println("arr1 after qsort: " );
printArr(arr1);
// randomly-generated arrays of n distinct vals
int[] arrN = new int[10];
for( int i = 0; i < arrN.length; i++ )
arrN[i] = i;
System.out.println("\narrN init'd to: " );
printArr(arrN);
shuffle(arrN);
System.out.println("arrN post-shuffle: " );
printArr(arrN);
qsort( arrN );
System.out.println("arrN after sort: " );
printArr(arrN);
//get-it-up-and-running, static test case w/ dupes:
int [] arr2 = {7,1,5,12,3,7};
System.out.println("\narr2 init'd to: " );
printArr(arr2);
qsort( arr2 );
System.out.println("arr2 after qsort: " );
printArr(arr2);
// arrays of randomly generated ints
int[] arrMatey = new int[20];
for( int i = 0; i < arrMatey.length; i++ )
arrMatey[i] = (int)( 48 * Math.random() );
System.out.println("\narrMatey init'd to: " );
printArr(arrMatey);
shuffle(arrMatey);
System.out.println("arrMatey post-shuffle: " );
printArr(arrMatey);
qsort( arrMatey );
System.out.println("arrMatey after sort: " );
printArr(arrMatey);
}//end main
*/
}//end class QuickSort