Recursive and non-recursive algorithm of binary lookup algorithm

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.