-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
102 lines (88 loc) · 3.3 KB
/
BinarySearch.java
File metadata and controls
102 lines (88 loc) · 3.3 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package sorting_searching;
import java.util.Comparator;
public class BinarySearch {
public static void main(String[] args) {
// Example:
int[] array = {27, 34, 39, 48, 70, 86, 87, 91, 95, 100};
System.out.println(binarySearchRecursive(array, 39)); // 2
System.out.println(binarySearchIterative(array, 39)); // 2
System.out.println(binarySearchRecursive(array, 40)); // -4 (not found, insertion point: 3)
System.out.println(binarySearchIterative(array, 40)); // -4 (not found, insertion point: 3)
}
public static int binarySearchRecursive(int[] a, int key) {
return binarySearchRecursive(a, key, 0, a.length);
}
private static int binarySearchRecursive(int[] a, int key, int left, int right) {
int n = right - left;
if (n == 0) {
return -right - 1;
}
int middle = n / 2 + left;
if (key == a[middle]) {
return middle;
}
if (key < a[middle]) {
return binarySearchRecursive(a, key, left, middle);
} else {
return binarySearchRecursive(a, key, middle + 1, right);
}
}
public static <T extends Comparable<? super T>> int binarySearchRecursive(T[] a, T key) {
return binarySearchRecursive(a, key, Comparator.naturalOrder());
}
public static <T> int binarySearchRecursive(T[] a, T key, Comparator<T> comp) {
return binarySearchRecursive(a, key, comp, 0, a.length);
}
private static <T> int binarySearchRecursive(T[] a, T key, Comparator<T> comp, int left, int right) {
int n = right - left;
if (n == 0) {
return -right - 1;
}
int middle = n / 2 + left;
if (key == a[middle]) {
return middle;
}
if (comp.compare(key, a[middle]) < 0) {
return binarySearchRecursive(a, key, comp, left, middle);
} else {
return binarySearchRecursive(a, key, comp, middle + 1, right);
}
}
public static int binarySearchIterative(int[] a, int key) {
return binarySearchIterative(a, key, 0, a.length - 1);
}
static int binarySearchIterative(int[] a, int key, int left, int right) {
while (left <= right) {
int middle = (left + right) / 2;
if (a[middle] == key) {
return middle;
}
if (key < a[middle]) {
right = middle - 1;
} else {
left = middle + 1;
}
}
return -left - 1;
}
public static <T extends Comparable<? super T>> int binarySearchIterative(T[] a, T key) {
return binarySearchIterative(a, key, Comparator.naturalOrder());
}
public static <T> int binarySearchIterative(T[] a, T key, Comparator<T> comp) {
return binarySearchIterative(a, key, comp, 0, a.length - 1);
}
static <T> int binarySearchIterative(T[] a, T key, Comparator<T> comp, int left, int right) {
while (left <= right) {
int middle = (left + right) / 2;
if (a[middle] == key) {
return middle;
}
if (comp.compare(key, a[middle]) < 0) {
right = middle - 1;
} else {
left = middle + 1;
}
}
return -left - 1;
}
}