-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinarySearch.java
52 lines (46 loc) · 1.58 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
package algorithms.search;
/**
* Time Complexity: O(log n) Logarithmic time
* Space Complexity: O(1) constant time
**/
public class BinarySearch {
static int recursiveBinarySearch(char[] data, char c) {
return binarySearch(data, c, 0, data.length);
}
private static int binarySearch(char[] data, char c, int start, int end) {
while (start < end) {
int mid = (start + end) / 2;
if (data[mid] == c) {
return mid;
} else if (data[mid] < c) {
return binarySearch(data, c, mid + 1, end);
} else return binarySearch(data, c, start, mid);
}
return -1;
}
static int iterativeBinarySearch(char[] data, char c) {
int left = 0;
int right = data.length;
while (left < right) {
int mid = (left + right) / 2;
if (data[mid] == c) {
return mid;
} else if (data[mid] < c) {
left = mid + 1;
} else right = mid;
}
return -1;
}
public static void main(String[] args) {
char[] data = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
System.out.println(String.valueOf(data));
int x = iterativeBinarySearch(data, 'e');
System.out.println("Loop: " + x);
x = iterativeBinarySearch(data, '6');
System.out.println("Loop: " + x);
x = recursiveBinarySearch(data, 'e');
System.out.println("Recursion: " + x);
x = recursiveBinarySearch(data, 'z');
System.out.println("Recursion: " + x);
}
}