-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinSearch.java
42 lines (31 loc) · 925 Bytes
/
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
public class BinSearch {
public static int binS(int[] arr, int low, int high, int key) {
if (String.valueOf(key) != null) {
if (low > high) {
return -1;
}
int mid = low + (high - low) / 2;
if (key > arr[mid]) {
return binS(arr, mid + 1, high, key);
}
if (key == arr[mid]) {
return mid;
}
if (key < arr[mid]) {
return binS(arr, low, mid - 1, key);
}
} else {
return -1;
}
return -1;
}
public static void main(String args[])
{
int input[]={1,3,3,5,6};
int m= binS(input,1,6,6);
if(m!=-1)
System.out.println("Element found at"+" "+m);
else
System.out.println("Element Not Found");
}
}