-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastSelect.java
More file actions
116 lines (102 loc) · 4.01 KB
/
FastSelect.java
File metadata and controls
116 lines (102 loc) · 4.01 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
//Adeebur Rahman
//APCS2 pd1
//HW14 -- So So Fast
//2017-03-07
/***
* class FastSelect
* includes method to obtain yth smallest integer in an array
* ALGORITHM
* 1. Starting with the last index as the pvtPos, call partition method on input array.
* 2. If index returned is equal to k-1, return value at the index.
* 3. If index is larger than k-1, return fastSelect but now with bounds [oldLeft,pivot).
* 4. If index is smaller, return fastSelect but now with bounds [pivot+1,oldRight].
*/
public class FastSelect {
//--------------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();
}
//--------------^ HELPER METHODS ^--------------
/******************************************************
* int partition(int[] arr,int left,int right,int pvtPos)
* Precond: Input array isn't empty,
* integer inputs within bounds of input array
* Postcond: Input array modified such that
* values smaller then number at pvtPos are to the left of pvtPos,
* and values larger are to the right of pvtPos,
* index at pvtPos is now at its final resting place.
* new index of pvtPos returned
******************************************************/
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;
}//end partition()
/******************************************************
* int fastSelect(int[] arr,int y)
* main fastSelect function
* Precond: Input array isn't empty,
* integer input is within bounds of input array.
* Postcond: Input array modified from partition,
* yth smallest int returned.
******************************************************/
public static int fastSelect(int[] arr, int y) {
return fastSelect(arr, 0, arr.length - 1, y);
}//end fastSelect()
/******************************************************
* int fastSelect(int[] arr,int left,int right,int y)
* helper method for fastSelect(int[], int)
* Precond: Input array isn't empty,
* integer inputs within bounds of input array,
* and left isn't greater than right.
* Postcond: Input array modified from partiton
* yth smallest int in [left, right] returned.
******************************************************/
public static int fastSelect(int[] arr, int left, int right, int y) {
int pivot = partition(arr,left,right,left+((right-left)/2));
//pivot = final resting place of yth lowest num. Return value at index pivot
if (pivot == y - 1) { return arr[pivot]; }
//pivot > final resting place. Restrict next call from left to pivot-1
else if (pivot > y - 1) { return fastSelect(arr,left,pivot-1,y); }
//pivot < final resting place. Restrict next call from pivot+1 to right
else { return fastSelect(arr,pivot+1,right,y); }
}//end fastSelect() (helper)
//main method for testing
public static void main(String args[]) {
//init test arrays
int[] p1 = {8,21,17,69,343};
int[] p3 = {1,28,33,4982,37};
int[] p4 = {5,4,17,9000,6};
int[] p5 = {3,0,16,599,1024};
int[][] arrs = {p1,p3,p4,p5};
// run fastSelect on each array, varying y
for (int i = 0; i < arrs.length; i++) {
System.out.print("Input Array: ");
printArr(arrs[i]);
for (int j = 1; j <= arrs[i].length; j++) {
System.out.print("y: ");
System.out.print(j);
System.out.print(" yth term: ");
System.out.println(fastSelect(arrs[i],j));
}
System.out.println("\n================================\n");
}
}//end main()
}//end class FastSelect