-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
42 lines (31 loc) · 792 Bytes
/
BinarySearch.java
File metadata and controls
42 lines (31 loc) · 792 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
35
36
37
38
39
40
41
42
import java.util.Arrays;
public class BinarySearch {
private BinarySearch() {}
public static int IndexOf(int[] A, int key){
int lo = 0;
int hi = A.length - 1;
while(lo <= hi){
int mid = lo + (hi-lo)/2;
if(key < A[mid]) hi = mid-1;
else if(key > A[mid]) lo = mid+1;
else return mid;
}
return -1;
}
public static void main(String[] args){
int A[] = {23, 432, 1, 44543, 330, 120, 342134, 550};
//Sort the array
Arrays.sort(A);
int index;
for(int i : A){
System.out.print(i + " ");
}
System.out.println();
index = IndexOf(A, 432);
System.out.println("Index of 432 : " + index);
index = IndexOf(A, 120);
System.out.println("Index of 120 : " + index);
index = IndexOf(A, 1);
System.out.println("Index of 1 : " + index);
}
}