-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearch1.java
36 lines (35 loc) · 955 Bytes
/
binarySearch1.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
class binarySearch1{
public static void main(String[] args){
int a[] = {-1,0,1,4,5,8};
int key = 0;
int low = 0;
// int loc = -1;
int high = a.length-1;
// System.out.println(12);
while(low <= high){
// System.out.println(23);
// if(low )
int mid = (low + high)/2;
mid = (int) Math.floor(mid);
if(a[mid] == key){
// loc = mid;
// break;
System.out.println(mid);
break;
}
else if(key > a[mid]){
low = mid + 1;
}
else if(key < a[mid]){
high = mid - 1;
}
}
if(low > high){
System.out.println("element not found");
}
// else{
// System.out.println(loc);
// }
// System.out.println("element not found");
}
}