-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathBinarySearch.java
66 lines (52 loc) · 1.62 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
56
57
58
59
60
61
62
63
64
65
66
/**
* LinearSearch: search a sorted array by resursively dividing the array into half.
*
* Time complexity: O(Logn).
* Space complexity:
* - Recursively: O(Logn).
* - Iteratively: O(1).
*
* Every time, compare x with the middle element of the array.
* - If x matches with the middle element, return the mid index;
* - Else if x is greater than the mid element, go to right half of the array;
* - Otherwise, go to the left half.
*
*/
public class BinarySearch {
public static int searchRecursively(int arr[], int x) {
return binarySearch(arr, 0, arr.length - 1, x);
}
public static int searchIteratively(int arr[], int x) {
return binarySearch(arr, x);
}
// Recursively
public static int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l)/2;
if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
// Iteratively
public static int binarySearch(int arr[], int x) {
int l = 0;
int r = arr.length - 1;
while (l <= r) {
int m = l + (r-l)/2;
if (arr[m] == x) return m;
if (arr[m] > x) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 10, 15, 20};
System.out.println(BinarySearch.search(arr, 10));
System.out.println(BinarySearch.search(arr, 1));
}
}