In computer science, binary searches ( English:half-interval Search), also called binary search Algorithms (binary searches ), dichotomy Binary search , binary exploration , is a search algorithm for finding a particular element in an ordered array. The search process begins with the middle element of the array, and if the intermediate element is exactly the element to be found, the search process ends, and if a particular element is greater than or less than the middle element, it is found in the half of the array greater than or less than the middle element, and is compared with the beginning from the middle element. If an array of steps is empty, the representation cannot be found. Each comparison of this search algorithm reduces the search scope by half.
Realize:
intBinarySearch (vector<int> &v,intx) { intleft =0; intright = V.size ()-1; while(Left <=Right ) { intMid = left + ((right-left) >>1); if(V[mid] = =x)returnmid; Else if(V[mid] <x) left= Mid +1; Elseright = mid-1; } return-1;}
Attention issues
- Interval opening and closing. Left and right represent the minimum subscript and maximum subscript for the array currently being processed by the algorithm, which uses the closed interval [0,v.size ()-1]. The decision condition of the closed interval is left<=right, if the last element with Left<right will jump out [Left,left].
- Intermediate subscript calculation. The average calculation can occur when the division performance is low, and the addition can overflow with MID = left + ((right-left) >>1) calculations avoidable.
- Subscript modification. Using +1-1 modification method, the new interval will not be the same as the original interval, the algorithm will not enter the dead loop.
Find first Occurrence
In the current interval of v [left,right], the data is non-descending order, if Tar<=v[mid], then the element to the right of V[mid] (not including v[mid]) is not all in compliance with the requirement; The left element includes V[mid] All are smaller than tar, then the leftmost = mid +1; when the loop condition is at the last element, a dead loop will occur, which needs to be changed to Left<right.
Like what:
Current situation: tar = = V[0], left = = 0,right = 0
Calculation: Mid = 0
Update: Update right = MID = 0;
intBinarysearchfirst (vector<int> & V,intx) { intleft =0; intright = V.size ()-1; while(Left <Right ) { intMid = left + ((right-left) >>1); if(x <=V[mid]) right=mid; Else Left= Mid +1; } if(V[left] = =x)returnLeft ; Else return-1;}
Find last Occurrence
In the current interval of v [left,right], the data is non-descending order, if Tar<v[mid], then v[mid] and the element on the right is not all in line with the requirements, then it = mid-1; if tar >= v[mid], the left element V[mid] All are smaller than tar, then left = Mid, but at the last two elements the loop will remain unchanged, so the method taken is int mid = left + ((Right-left + 1) >>1);
Like what:
Current situation: tar = = V[0], left = = 0,right = 1
Calculation: Mid = 0
Update: Update left = MID = 0;
intBinarysearchlast (vector<int> &v,intx) { intleft =0; intright = V.size ()-1; while(Left <Right ) { intMid = left + ((Right-left +1) >>1); if(X <V[mid]) right= Mid-1; Else Left=mid; } if(V[left] = =x)returnLeft ; Else return-1;}
Algorithm Review notes: Two-point search