-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
83 lines (76 loc) · 3.16 KB
/
BinarySearch.java
File metadata and controls
83 lines (76 loc) · 3.16 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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Comparator;
/**
* A collection of Binary Searches, with each method and method overloads as
* an implementation.
*/
public class BinarySearch <T> {
/******************** First Binary Search Implementation ********************/
/**
* A binary search, assumes that data is sorted.
* @return True if the target value is found in the indicated position of the
* data array. Searches through array portion of [lo,hi] inclusive in data.
*/
public static boolean binarySearch(int[] data, int target, int lo, int hi){
if(lo > hi) { return false; } // Interval Empty; No Match
else {
int mid = lo + hi >>> 1; // mid = (lo+hi)/2 , always positive result
if (target == data[mid]){
return true; // Found Match
} else if (target < data[mid]){
return binarySearch(data, target, lo, mid-1); // Search left of middle
} else {
return binarySearch(data, target, mid+1, hi); // Search right of middle
}
}
}
/**
* A binary search with a cleaner signature
* @param data The array to search in
* @param target The target to search for
* @return true if target value is found in the data array
*/
public static boolean binarySearch(int[] data, int target){
return binarySearch(data, target, 0, data.length -1);
}
/******************** Second Binary Search Implementation ********************/
// This is the same as the First implementation but uses generic data type
Comparator<T> comp = (T t1, T t2) -> compare(t1,t2);
/**
* Compares Two Keys by natural ordering
* @param x First key to compare
* @param y Second key to compare
* @return -1 if x < y,
* 0 if x==y,
* 1 if x > y
*/
protected int compare(T x, T y){
return comp.compare(x,y);
}
/**
* A binary search, assumes that data is sorted.
* @return True if the target value is found in the indicated position of the
* data array. Searches through array portion of [lo,hi] inclusive in data.
*/
public boolean binarySearch(T[] data, T target, int lo, int hi){
if(lo > hi) { return false; } // Interval Empty; No Match
else {
int mid = lo + hi >>> 1; // mid = (lo+hi)/2 , always positive result
if (comp.compare(target,data[mid]) == 0){
return true; // Found Match
} else if (comp.compare(target,data[mid]) < 0){
return binarySearch(data, target, lo, mid-1); // Search left of middle
} else {
return binarySearch(data, target, mid+1, hi); // Search right of middle
}
}
}
/**
* A binary search with a cleaner signature
* @param data The array to search in
* @param target The target to search for
* @return true if target value is found in the data array
*/
public boolean binarySearch(T[] data, T target){
return binarySearch(data, target, 0, data.length -1);
}
}