-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarySearch.java
More file actions
56 lines (47 loc) · 1.93 KB
/
Copy pathBinarySearch.java
File metadata and controls
56 lines (47 loc) · 1.93 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
// Given a sorted (in ascending order) integer array nums of n elements and a target value,
// write a function to search target in nums. If target exists,
// then return its index, otherwise return -1.
// See: https://leetcode.com/problems/binary-search/
package leetcode.binary_search;
public class BinarySearch {
// Recursive solution
// Note: after the iterative solution the recursive was trivial
public int search(int[] nums, int target) {
return bs(nums, target, 0, nums.length - 1);
}
public int bs(int[] nums, int target, int start, int end) {
if (start > end) return -1;
int guessIndex = (start + end) / 2;
if (nums[guessIndex] == target) {
return guessIndex;
} else if (nums[guessIndex] < target) {
return bs(nums, target, guessIndex + 1, end);
} else {
return bs(nums, target, start, guessIndex - 1);
}
}
// Iterative approach
public int search1(int[] nums, int target) {
int start = 0, end = nums.length - 1;
while (start <= end) {
int guessIndex = (start + end) / 2;
if (nums[guessIndex] == target) {
return guessIndex;
} else if (nums[guessIndex] < target) {
start = guessIndex + 1;
} else {
end = guessIndex - 1;
}
}
return -1;
}
public static void main(String[] args) {
BinarySearch sln = new BinarySearch();
System.out.println(sln.search(new int[] { -1, 0, 3, 5, 9, 12 }, 9));
System.out.println(sln.search(new int[] { -1, 0, 3, 5, 9, 12 }, -1));
System.out.println(sln.search(new int[] { -1, 0, 3, 5, 9, 12 }, 12));
System.out.println(sln.search(new int[] { -1, 0, 3, 5, 9, 12 }, 20));
System.out.println(sln.search(new int[] { -1, 0, 3, 5, 9, 12 }, 2));
System.out.println(sln.search(new int[] { 5 }, 5));
}
}