-
Notifications
You must be signed in to change notification settings - Fork 3
/
BinSearch.java
134 lines (113 loc) · 4.35 KB
/
BinSearch.java
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
/*
Team Incredibly Cohesive (David Chen, Jaylen Zeng, Orion Roven)
APCS pd7
L03: Get Empirical
2021-12-16
time spent: .6 hrs
DISCO:
QCC:
*/
public class BinSearch {
/**
* int binSearch(Comparable[],Comparable) -- searches an array of
* Comparables for target Comparable
* pre: input array is sorted in ascending order
* post: returns index of target, or returns -1 if target not found
**/
public static int binSearch(Comparable[] a, Comparable target) {
// uncomment exactly 1 of the 2 stmts below:
return binSearchIter(a, target, 0, a.length - 1);
// return binSearchRec( a, target, 0, a.length-1 );
}
public static int binSearchRec(Comparable[] a,
Comparable target,
int lo, int hi) {
int tPos = -1; // init return var to flag value -1
int m = (lo + hi) / 2; // init mid pos var
if (lo <= hi) {
if (target.compareTo(a[m]) == 0) {
tPos = m;
return tPos;
} else if (target.compareTo(a[m]) < 0) {
tPos = binSearchRec(a, target, lo, m - 1);
} else {
tPos = binSearchRec(a, target, m + 1, hi);
}
}
return tPos;
}// end binSearchRec
public static int binSearchIter(Comparable[] a,
Comparable target,
int lo, int hi) {
int tPos = -1; // init return var to flag value -1
int m = (lo + hi) / 2; // init mid pos var
while (lo <= hi) {
m = (lo + hi) / 2;
if (a[m].compareTo(target) == 0) {
tPos = m;
break;
} else if (a[m].compareTo(target) > 0) {
hi = m - 1;
} else {
lo = m + 1;
}
}
return tPos;
}// end binSearchIter
// tell whether an array is sorted in ascending order
private static boolean isSorted(Comparable[] arr) {
boolean retBoo = true; // init to true, assume array is sorted
// Q: Why would a FOREACH loop not suffice here?
for (int i = 0; i < arr.length - 1; i++) {
if ((arr[i].compareTo(arr[i + 1]) > 0)) {
return false;
}
}
return retBoo; // if entire array was traversed, it must be sorted
}
// utility/helper fxn to display contents of an array of Objects
private static void printArray(Comparable[] arr) {
String output = "[ ";
for (Comparable c : arr)
output += c + ", ";
output = output.substring(0, output.length() - 2) + " ]";
System.out.println(output);
}
// main method for testing
// minimal -- augment as necessary
public static void main(String[] args) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
System.out.println("\nNow testing binSearch on Comparable array...");
// Declare and initialize array of Comparables
Comparable[] iArr = { 2, 4, 6, 8, 6, 42 };
printArray(iArr);
System.out.println("iArr1 sorted? -- " + isSorted(iArr));
Comparable[] iArr2 = { 2, 4, 6, 8, 13, 42 };
printArray(iArr2);
System.out.println("iArr2 sorted? -- " + isSorted(iArr2));
Comparable[] iArr3 = new Integer[10000];
for (int i = 0; i < iArr3.length; i++) {
iArr3[i] = i * 2;
}
printArray(iArr3);
System.out.println("iArr3 sorted? -- " + isSorted(iArr2));
// search for 6 in array
System.out.println(binSearch(iArr2, 2));
System.out.println(binSearch(iArr2, 4));
System.out.println(binSearch(iArr2, 6));
System.out.println(binSearch(iArr2, 8));
System.out.println(binSearch(iArr2, 13));
System.out.println(binSearch(iArr2, 42));
// search for 43 in array
System.out.println(binSearch(iArr2, 43));
System.out.println("now testing binSearch on iArr3...");
System.out.println(binSearch(iArr3, 4));
System.out.println(binSearch(iArr3, 8));
System.out.println(binSearch(iArr3, 5));
// search for 43 in array
System.out.println(binSearch(iArr3, 43));
/*----------------------------------------------------
====================================================*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}// end main()
}// end class BinSearch2