/
BinarySearch.java
40 lines (30 loc) · 959 Bytes
/
BinarySearch.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
/**
* Search the array for the binary search
*/
public class BinarySearch {
/**
* return the index of the value searched or -1 if not found
* assumtion that array is sorted
*/
public static int binarySearch(int[] testInput, int target) {
int low = 0;
int high = testInput.length -1;
while (low <= high) {
int mid = low + ((high -low) / 2);
if (testInput[mid] == target) {
return mid;
}
else if (target > testInput[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
assert binarySearch(new int[]{0, 5, 13, 19, 22, 41, 55, 68, 72, 81, 98}, 55) == 6;
assert binarySearch(new int[]{0, 5, 13, 19}, 21) == -1;
assert binarySearch(new int[]{5, 13, 44, 399, 722}, 722) == 4;
}
}