-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathBinarySearch.java
55 lines (41 loc) Β· 1.01 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package dsaLecture;
public class BinarySearch {
public static int binarySearch(int[] arr, int key) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (key == arr[mid]) {
return mid;
}
if (key > arr[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
public static int binSearchRecursive(int[] arr, int key) {
return binSearchRecursive(arr, key, 0, arr.length - 1);
}
private static int binSearchRecursive(int[] arr, int key, int low, int high) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (key == arr[mid]) {
return mid;
}
if (key > arr[mid]) {
return binSearchRecursive(arr, key, mid + 1, high);
} else {
return binSearchRecursive(arr, key, low, mid - 1);
}
}
public static void main(String[] args) {
int[] arr = { -1, 2, 5, 7, 11, 14 };
int key = 2;
System.out.println(binarySearch(arr, key)); // 1
System.out.println(binSearchRecursive(arr, key)); // 1
}
}