Basic knowledge
Two-part non-recursive notation:
int binary_search (const int a[], const int size, const int val) { int lower = 0; int upper = size-1; /* Invariant:if a[i]==val for no I, then lower <= i <= Upper * * While (lower <= Upper) { int i = lower + (upper-lower) >>1; if (val = = A[i]) { return i; } else if (Val < a[i]) { upper = i-1; } else {/* val > A[i] */ Lo Wer = i+1; } } return-1;}
Two-part recursive notation
int BinarySearch (int *array, int left, int. right, int value) { if (left > right) { //value not found retur n-1; } int mid = left + (right-left)/2; if (array[mid] = = value) { return mid; } else if (Array[mid] < value) { return BinarySearch (array, mid + 1, right, value); } else { return BinarySearch (array, left, mid-1, value);} }
Brush question 1 Find a Fixed point in a given array
http://www.geeksforgeeks.org/find-a-fixed-point-in-a-given-array/
Given an array of n distinct integers sorted in ascending order, write a function, this returns a Fixed point in the array, If there is a Fixed point present in array, else returns-1. The Fixed point in an array is a index i such that arr[i] was equal to I. Note that integers in array can be negative.
Examples:
Input:arr[] = { -10, -5, 0, 3, 7} output:3 //arr[3] = = 3 input:arr[] = {0, 2, 5, 8, +} output:0 / /arr[0] = = 0
Input:arr[] = { -10, -5, 3, 4, 7, 9} Output:-1 //No Fixed Point
Look at the code:
int indexsearch (int *array, int left, int.) { if (left > right) { //value not found return-1; } int mid = right-(right-left)/2; if (array[mid] = = Mid) { return mid; } else if (Array[mid] < mid) { return Indexsearch (array, mid + 1, righ t); } else { return Indexsearch (array, left, mid-1);} }
2 Search Insert Position
https://leetcode.com/problems/search-insert-position/
Given a sorted array and a target value, Return the index if the target is found. If not, return the index where it would is if it were inserted in order.
Assume no duplicates in the array.
Here is few examples.
[1,3,5,6] , 5→2
[1,3,5,6] , 2→1
[1,3,5,6] , 7→4
[1,3,5,6]< /code>, 0→0
/** * Assume that the sorted array is: A b c d E F g * If you exclude D e f G in the following way, it is guaranteed that the target is no longer in [d,g], but not in [a,c], so when left <= Right, there is a target not [the leftmost element, left adjacent element] and [right side adjacent element, rightmost element], the following conditions are analyzed: * * 1. * When left goes right, the element is ruled out, and the target itself is not in the [right-hand side of right, the right-most element], so there is nums[right] < target < Nums[left] * * 2. * When right goes to left, the element is ruled out, and the target itself is not [the leftmost element, left adjacent element is now right], * therefore nums[right] < target < Nums[left] * * All in all: * When right < Left, it is known that target is not in the [right-hand side of the element], and [on the side], so there must be nums[right] < target < Nums[left ] */class Solution {public:int Searchinsert (vector<int>& nums, int target) {int left = 0; int right = Nums.size ()-1; while (left <= right) {int middle = left + ((right-left) >> 1); if (target = = Nums[middle]) {return middle; } else if (Target < Nums[middle]) {right = Middle-1; } else { left = middle + 1; }}//Note: The Nums[right] < target < Nums[left]//Boundary condition must be guaranteed when right < Left exits the loop, when right =-1, with target < nums[left] = Nums[0]//Boundary condition, when left = Nums.size (), there is nums[nums.size ()-1] = Nums[right] > Targ ET return right + 1; }};3 Search in rotated Sorted Array
https://leetcode.com/problems/search-in-rotated-sorted-array/
Suppose a sorted array is rotated on some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2 ).
You is given a target value to search. If found in the array is return its index, otherwise return-1.
Assume no duplicate exists in the array.
Ideas:
1. After rotate, the ordered array is divided into two ordered arrays.
2. After two minutes, [Left,middle] and (Middle,end] must have an orderly. We can exclude elements by an ordered array of them.
Now suppose [Left,middle] the elements in this interval are ordered, and if key is in the [Left,middle] interval, the elements (Middle,end] can be excluded, and if key is not in the [Left,middle] interval, then [left, Middle) excluded.
3. It is not difficult to find, as long as the middle has one side of the elements are ordered, you can use two points.
Class Solution {Public:int search (vector<int>& nums, int target) {int left = 0; int right = Nums.size ()-1; while (left <= right) {int middle = left + ((right-left) >> 1); if (target = = Nums[middle]) {return middle; }//Left half ordered if (Nums[left] <= Nums[middle]) {if (Nums[left] <= Target && Target < Nums[middle]) {right = Middle-1; } else {left = middle + 1; }}//Right half ordered if (Nums[middle] < Nums[right]) {if (Nu Ms[middle] < target && target <= Nums[right]) {left = middle + 1; } else {right = middle-1; }}} return-1; }};4 Sqrt (x)
https://leetcode.com/problems/sqrtx/
Implement int sqrt(int x) .
Compute and return the square root of X.
Class Solution {public: int mysqrt (int x) { if (x = = 0) { return x; } int left = 1; int right = x; while (left <= right) { int middle = left + ((right-left) >> 1); if (middle = = X/middle) { return middle; } else if (middle < x/middle) {left = middle + 1; } els e {Right = middle-1; } } return right; };
The following are the floating-point versions:
Double Mysqrthelper (double x, double lowbound, double highbound) { double precision = 0.00001; Double sqrt = lowbound/2 + highbound/2; if (ABS (SQRT * sqrt-x) < precision) { return sqrt; } else if (sqrt * sqrt-x > 0) { return mysqrt Helper (x, Lowbound, sqrt); } else { return mysqrthelper (x, sqrt, highbound);} } Double mysqrt (double x) { if (x < 0) return ERROR; if (x = = 0) { return 0; } if (x = = 1) { return 1; } if (x < 1) { return mysqrthelper (x, X, 1); } else { return mysqrthelper (x, 1, x);} }
5 Matrix Search
BOOL Iselementinmatrix (int **matrix, int M, int N, int target) { int row = 0; int column = N-1; while (Row < M && column >= 0) { if (matrix[row][column] = = target) { return true; } else if (MA Trix[row][column] < target) { row++; } else { column--; } } return false;}
Complexity O (m+n).
6 Range Search
void Searchrangehelper (int array[], int left, int. right, int target, int &begin, int. &end) {if (Left > Righ T) {return; } int mid = right-(right-left)/2; if (array[mid] = = target) {if (Mid < begin | | begin = =-1) {begin = Mid; } if (Mid > End) {end = Mid; } searchrangehelper (array, left, mid-1, target, begin, end); Searchrangehelper (Array, mid + 1, right, target, begin, end); } else if (Array[mid] < target) {Searchrangehelper (array, mid + 1, right, target, begin, end); } else {Searchrangehelper (array, left, mid-1, target, begin, end); }}vector<int> searchrange (int a[], int n, int target) {int begin =-1, end =-1; Searchrangehelper (A, 0, n-1, target, begin, end); Vector<int> ans; Ans.push_back (begin); Ans.push_back (end); return ans;}
(not to be continued)
[algorithm topic] Binary search & Sort array