-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
159 lines (128 loc) · 4.19 KB
/
QuickSort.java
File metadata and controls
159 lines (128 loc) · 4.19 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
//Shafali Gupta
//APCS2 pd01
//HW18 -- QuickSort
//2018-03-13t
/*****************************************************
* class QuickSort
* Implements quicksort algo to sort an array of ints in place
*
* 1. Summary of QuickSort algorithm:
* QSort(arr):
*Find the final position of the last element of the unsorted list using partition.
*Then, you split the array into two, based on the pivot and recursively split it into two, using partition each time.
* 2a. Worst pivot choice and associated runtime:
* pivot is the first or last element, runtime of n^2
* 2b. Best pivot choice and associated runtime:
*pivot is the middle, nlog(n)
* 3. Approach to handling duplicate values in array:
* don't swap unless the value is less than the pivot.
*****************************************************/
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
*****************************************************/
public static void qsort( int[] d )
{
QuickSort(d, 0, d.length -1);
}
public static int[] QuickSort(int[] d, int low, int high){
if (low< high){
int pivot= partition(d, low, high);
QuickSort(d, low, pivot - 1);
QuickSort(d, pivot +1, high);
}
return d;
}
public static int partition( int arr[], int left, int right)
{
int v = arr[right];
int i = left - 1;
for (int j = left; j< right; j++){
if (arr[j] < v){
i +=1;
swap(i, j, arr);
}}
swap(i+1, right, arr);
return i+1;
}//end partition
//you may need a helper method...
//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);
/*~~~~s~l~i~d~e~~~m~e~~~d~o~w~n~~~~~~~~~~~~~~~~~~~~ (C-k, C-k, C-y)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//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);
/*~~~~s~l~i~d~e~~~m~e~~~d~o~w~n~~~~~~~~~~~~~~~~~~~~ (C-k, C-k, C-y)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
}//end main
}//end class QuickSort