Binary Search Algorithm

Source: Internet
Author: User

Concept: Binary Search, also known as half-fold search, has the advantage of a relatively small number of times, fast search speed, and good average performance. Its disadvantage is that The table to be queried must be an ordered table.And insertion and deletion are difficult. Therefore, the half-fold search method is applicable Searches for frequently ordered lists without frequent changes. First, Assume that the elements in the table are arranged in ascending order., Compares the keywords recorded in the table's intermediate position with the search keyword. If the two are the same, the search is successful. Otherwise, the table is divided into the first and last sub-tables using the intermediate position record, if the keyword recorded in the middle position is greater than the search keyword, the previous sub-table is further searched; otherwise, the next sub-table is further searched. Repeat the preceding process until you find a record that meets the conditions to make the search successful, or until the child table does not exist, the search fails.

Implementation:

/*************************************** * ****** Binary Search Algorithm by rowandjj2014/7/11 ************************** * *******************/# include <iostream> using namespace STD; // binary search algorithm. Arr is the array to be queried, Len is the array length, and target is the element int binary_search (INT arr [], int Len, int target); int main () {int t; int arr [] = {-,}; int arr2 [] = {-,}; CIN> T; cout <binary_search (arr2, 5, T) <Endl; return 0;} // ensure that the array is ordered and INT binary_search (INT arr [], int Len, int target) {int low = 0, high = len-1, middle; while (low <= high) {middle = low + (high-low)> 1); // prevents overflow and efficiency. it is not recommended to use middle = (high + low)/2if (ARR [Middle] = target) {return middle;} else if (ARR [Middle]> target) // find {high = middle-1;} else // arr [Middle] <target in the first half. Find {LOW = middle + 1;} in the second half ;}} return-1 ;}

Test:


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.