-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
28 lines (25 loc) · 1 KB
/
Copy pathBinarySearch.java
File metadata and controls
28 lines (25 loc) · 1 KB
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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class BinarySearch {
private static int c = 0;
public static int search(String[] strings, String string, int l, int h) {
int mid = l + (h - l) / 2;
c++;
if (h <= l) return -1;
else if (strings[mid].compareTo(string) > 0)
return search(strings, string, l, mid);
else if (strings[mid].compareTo(string) < 0)
return search(strings, string, mid + 1, h);
else return mid;
}
public static void main(String[] args) {
String[] strings = {
"blueberry", "chocolate", "coconut", "coffee", "mint", "strawberry", "vanilla"
};
int i = search(strings, "mint", 0, 6);
System.out.println(i + " ( " + c + " times search)");
}
}