Binary search is the algorithm we often encounter, clear thinking, code concise. Binary lookup requires sequential ordering, and support random access, generally we discuss the sequence does not exist the same element, then the binary search can be very skilled to express the following:
int binsearch (int a[], int n, int target) { int left=0,right=n-1,res=-1; while (Left<=right) { int mid = left+ ((right-left) >>1); if (a[mid]<target) left = mid+1; else if (a[mid]>target) Right = mid-1; else { return mid; } } return-1; }
However, when we do not require the ordered sequence, that is, the same elements may appear, the binary lookup will be back to some of the expansion problems, such as:
- Returns the index of the first occurrence of key, if no return-1;
- Returns the last occurrence of the key, if no return-1;
- Returns the subscript of an element that is just less than key, if no 1 is returned;
- Returns the subscript of an element that is just greater than key, if no 1 is returned;
for (1), when the element with the same key is found, it is not returned immediately, but is recorded first, and then continues to look for the same element on the left that has the same key.
int searchlowerbound (int a[], int n, int target) { int left=0,right=n-1,res=-1; while (Left<=right) { int mid = left+ ((right-left) >>1); if (a[mid]<target) left = mid+1; else if (a[mid]>target) Right = mid-1; else { res = mid; right = Mid-1; } } return res; }
for (2), the same can be done by:
int searchhigherbound (int a[], int n, int target) { int left=0,right=n-1,res=-1; while (Left<=right) { int mid = left+ ((right-left) >>1); if (a[mid]<target) left = mid+1; else if (a[mid]>target) Right = mid-1; else { res = mid; left = mid+1; } } return res; }
for (3), making key=key-1, the conversion is problematic (2), for (4), making key=key+1, then the conversion is problematic (1).
Let me take a look at the two points that appear in a leetcode as an example:
search for a rangeTitle Description:
Given a sorted array of integers, find the starting and ending position of a Given target value. Your algorithm ' s runtime complexity must is in the order of O (log n). If the target is not a found in the array, return [-1,-1].
For example,
Given [5, 7, 7, 8, 8, ten] and target value 8,
return [3, 4].
The problem is to ask for the lowest value of the subscript and the largest value subscript, so the code is easy to get:
Class Solution {Public:int searchlowerbound (int a[], int n, int target) {int left=0,right=n-1,res=-1; while (left<=right) {int mid = left+ ((right-left) >>1); if (a[mid]<target) left = mid+1; else if (a[mid]>target) right = mid-1; else {res = mid; right = Mid-1; }} return res; } int searchhigherbound (int a[], int n, int target) {int left=0,right=n-1,res=-1; while (left<=right) {int mid = left+ ((right-left) >>1); if (a[mid]<target) left = mid+1; else if (a[mid]>target) right = mid-1; else {res = mid; left = mid+1; }} return res; } vector<int> searchrange (int a[], int n, int target) {VECTOR<INT> Res Res.push_back (Searchlowerbound (a,n,target)); Res.push_back (Searchhigherbound (a,n,target)); return res; }};
To say two points to find