-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_704_binary_search.java
More file actions
44 lines (38 loc) · 1.32 KB
/
Copy path_704_binary_search.java
File metadata and controls
44 lines (38 loc) · 1.32 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
public class _704_binary_search {
// Runtime: 0 ms, faster than 100.00% of Java online submissions for Binary Search.
public static int binary_search(int[] a, int key) {
int n = a.length;
int iLeft = 0;
int iRight = n-1;
while (iLeft <= iRight) {
int iMid = (iLeft + iRight)/2;
if (a[iMid] == key) {
return iMid;
} else if (a[iMid] < key) {
iLeft = iMid + 1;
} else {
iRight = iMid -1;
}
}
return -1; // not found
}
// Runtime: 0 ms, faster than 100.00% of Java online submissions for Binary Search.
public static int binary_search_recursion(int[] a, int key, int iLeft, int iRight) {
if (iLeft > iRight) {
return -1;
}
int iMid = (iLeft + iRight)/2;
if (a[iMid] == key) {
return iMid;
} else if (a[iMid] < key) {
return binary_search_recursion(a, key, iMid+1, iRight);
} else {
return binary_search_recursion(a, key, iLeft, iMid-1);
}
}
public static void main(String[] args) {
int[] a = {-1,0,3,5,9,12};
System.out.println(binary_search(a, 9));
System.out.println(binary_search_recursion(a, 9, 0, a.length-1));
}
}