Detailed explanation of three common solutions to binary search
704.Binary search _
Given an n-element ordered (ascending) integer array nums and a target value target, write a function to search for target in nums and return the subscript if the target value exists, otherwise -1.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 appears in nums with subscript 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so -1 is returned
hint:
You can assume that all elements in nums are unique.
n will be between [1, 10000].
Each element of nums will be between [-9999, 9999].
1. Dichotomy - basic version (left closed and right closed interval)
class Solution {
public int search(int[] nums, int target) {
int i = 0,j = nums.length - 1;
while(i <= j){
int m = (i + j) >>> 1;
if(target < nums[m]){
j = m - 1;
}else if(nums[m] < target){
i = m + 1;
}else{
return m;
}
}
return -1;
}
}
i
andj
point to the starting and ending positions of the array respectively- The loop condition is that
i <= j
as long as the array has not been traversed, the search can continue - Use bit operations
>>> 1
to implement division2
operations to preventright + left
exceedingint
the range of - If the target value
target
is less than the middle valuenums[m]
, it means that the target value is to the left of the middle value,j
update the end position tom - 1
, and continue searching for the left half - If the middle value
nums[m]
is less than the target valuetarget
, it means that the target value is to the right of the middle value,i
update the starting position tom + 1
, and continue searching for the right half - If the intermediate value
nums[m]
is equal to the target valuetarget
, it means that the target value is found and the index is returned directly.m
- If the target value is still not found at the end of the loop, it returns
-1
, indicating that the target value does not exist in the array.
2. Dichotomy - improved version (left closed and right open interval)
class Solution {
public int search(int[] nums, int target) {
int i = 0,j = nums.length;
while(i < j){
int m = (i + j) >>> 1;
if(target < nums[m]){
j = m;
}else if(nums[m] < target){
i = m + 1;
}else{
return m;
}
}
return -1;
}
}
i
andj
respectively point to起始位置
the sum of the arrays右边界
- Use bit operations
>>> 1
to implement division2
operations to preventright + left
exceedingint
the range of - The loop condition is that
i < j
as long as the array has not been traversed, the search can continue - If the target integer is less than
nums[m]
, it means that the target integer is onm
the left side of ,j
adjust to m - If the target integer is greater than
nums[m]
, it means that the target integer is onm
the right side of ,i
adjust tom + 1
- If the target integer is equal to the target integer
nums[m]
, it means that the target integer is found and the index is returned directly.m
- If the target integer is not found at the end of the loop, return
-1
means not found
3. Dichotomy - balanced version (left closed and right open interval)
class Solution {
public int search(int[] nums, int target) {
int i = 0,j = nums.length;
while(i + 1 < j){
int m = (i + j) >>> 1;
if(target < nums[m]){
j = m;
}else{
i = m;
}
}
return (nums[i] == target) ? i : -1;
}
}
- The loop condition is
i + 1 < j
that the starting position pointeri
is alwaysj
smaller than the end position pointer1
, ensuring that in the last loop, the pointeri
points to the last possible position of the target integer. - If the target integer is found,
i
the value of the pointer is returned, otherwise it is returned-1
to indicate that it was not found.