-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinarySearch.java
More file actions
34 lines (26 loc) · 843 Bytes
/
BinarySearch.java
File metadata and controls
34 lines (26 loc) · 843 Bytes
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
/**
* Created by jkeys on 12/3/2015.
*/
public class search {
public static int binarySearch(int[] A, int e) {
int low = 0;
int high = A.length - 1;
while(low < high) {
int mid = (low + high) / 2;
if(A[mid] == e)
return mid;
else if(A[mid] < e)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static void main(String[] args) {
int[] a = { 2, 4, 6, 8, 10, 20, 50, 100, 250, 500, 1000};
System.out.println(binarySearch(a, 2)); //should print "0"
System.out.println(binarySearch(a, 20)); //should print "5"
System.out.println(binarySearch(a, 250)); //should print "8"
System.out.println(binarySearch(a, 133)); //should print "-1"
}
}