forked from anitaa1990/Android-Cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.java
54 lines (41 loc) · 1.37 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
package search;
public class BinarySearch {
/*
* Binary Search using recursion
* Time complexity - Olog(n)
* Memory Complexity - O(logn) - as it will consume memory on the stack
* */
private static int recursiveBinarySearch(int[]arr, int n, int low, int high) {
if(low > high) {
return - 1;
}
int middle = low + ((high-low)/2);
if(arr[middle] == n) return middle;
else if(n > arr[middle]) {
return recursiveBinarySearch(arr, n, middle+1, high);
} else return recursiveBinarySearch(arr, n, low, middle-1);
}
/*
* Binary Search using iteration
* Time complexity - Olog(n)
* Memory Complexity - Constant, O(1)
* */
private static int iterativeBinarySearch(int[]arr, int n, int low, int high) {
while (low <= high) {
int middle = low + ((high-low)/2);
if(arr[middle] == n) return middle;
if(n > arr[middle]) {
low = middle+1;
} else {
high = middle-1;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1,11,23,34,45,56,67,78,89,90};
int n = 90;
System.out.println(recursiveBinarySearch(arr, n, 0, arr.length-1));
System.out.println(iterativeBinarySearch(arr, n, 0, arr.length-1));
}
}