Their contact and understanding of the search algorithm summed up into 3 bar:
1. Static lookup (mainly binary search, high efficiency)
2. Dynamic lookup (two-fork lookup tree)
3. Hash table
First of all, the two-point search bar.
Basic idea: Suppose an ordered sequence (n elements) in a sorted order, in ascending order), first let the keywords in the sequence n be compared to the keyword that needs to be searched, and if they are equal, the search succeeds, otherwise the sequence is divided into two subsequence by using the middle position, if the keyword to be searched is less than the middle keyword. The same method is further searched in the previous subsequence, and if the keyword to be searched is greater than the middle keyword, the same method is further searched in the latter sequence until the end of the lookup is repeated.
Time complexity: O (log (n))
Complexity of Space: O (1)
code example:
#include <stdio.h>//Two find non-recursive implementation int BINSEARCH1 (int array[], int low, int high, int key) {int ret =-1;
int mid = 0;
ret = (Array!= NULL) && (Low >= 0) && (High > Low);
printf ("Search Key:%d\n", key);
if (ret) {While [low <= high] {mid = (low + high)/2;
if (array[mid] = = key) {ret = mid; printf ("BINSEARCH1 success!!!
\NARRAY[%D] = key\n ", mid);
return ret;
else if (Array[mid] > key) {high = mid-1;
else {low = mid + 1;
}} return ret;
///two find recursive implementation int BINSEARCH2 (int array[], int low, int high, int key) {int ret =-1;
int mid = 0; ret = (Array!= NULL) && (Low >= 0) && (High > Low);
if (ret)//{if (low <= high) {mid = (low + high)/2;
if (array[mid] = = key) {return mid;
} if (Array[mid] > key) return BinSearch2 (Array, Low, mid-1, key);
if (Array[mid] < key) return BinSearch2 (Array, mid+1, High, key);
else {return-1;
}//}//return ret;
int main (int argc, char *argv[]) {int array[10] = {1, 3, 5, 6, 7, 8, 10, 13, 15, 17};
int A;
A = BinSearch1 (array, 0, 10, 13);
A = BINSEARCH2 (array, 0, 10, 13);
printf ("A =%d\n", a);
printf ("array[%d] =%d\n", A, array[a]);
printf ("Press ENTER to continue ...");
GetChar ();
return 0;
}
Algorithm thinking is simpler, here is not to write comments, recursive code more concise, understanding is also slightly easier, the premise is a profound understanding of recursive thinking, the topic of recursion to fill up ...
Screenshot of Run Effect:
Recursive code works the same as above.