Base algorithm-find: Binary find

Source: Internet
Author: User

binary Find

Also known as binary search. This lookup method requires that the data for the lookup table be stored in a linear structure, and that the data in the lookup table is ordered from small to large in a keyword.

Binary lookup (binary lookup) is a simple and efficient search algorithm with a lookup length of up to ㏒2n+1 (the depth of the decision tree), an average lookup length of ㏒2 (n+1)-1, and a higher efficiency than the sequential lookup, but binary lookup can only be applied to sequentially stored ordered tables (for example, a linear list cannot be effectively binary-searched).

Implementation of classical non-recursive algorithm

int binary_search (int search_table[], int key, int low, int. high) {and low <= high} {Mid  = (low + high)/2;if (Sear Ch_table[mid] < key) {low = mid + 1;} else if (Search_table[mid] > key) {high = mid-1;} Else{return Mid;}} whilereturn-1;}

Implementation of classical recursive algorithm

int binary_search (int search_table[], int key, int low, int. high) {if (Low > High) {return-1;} int mid = (low + high)/2;if (search_table[mid] = = key) {return mid;} else if (Search_table[mid] < key) {Binary_search (search_table, Key, Mid + 1, high);} Else{binary_search (search_table, Key, Low, mid-1);}}

However, as the precondition for binary lookup is that ordered table sequential storage is required, for static lookup tables, once the sorting is no longer changed, such an algorithm is better. However, for datasets that frequently perform insert or delete operations, maintaining an ordered ordering can have a small amount of work, which is not recommended.

Base algorithm-find: Binary find

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.