Binary lookup algorithm (recursive, loop)

Source: Internet
Author: User

The binary lookup algorithm isordered ArrayA more frequent algorithm used in the non-contact binary search algorithm, the most common approach is to iterate the array, compared to each element, its time isO (n).but the binary lookup algorithm is better, because its lookup time isO (LGN), such as arrays{1,2,3,4,5,6,7,8,9}, find elements6, the binary lookup algorithm is executed in the order of:
1.The first step is to find the middle element,5, since5<6, you6must be in5after the array element, then in the{6,7,8,9}in the Find,
2.Looking for{6,7,8,9}The median number of7,7>6, you6should be in7to the left of the array element, then only the remaining6, that was found.

The Two-point lookup algorithm is keep the array in half-split, each timetake the middle element andTarget Elementto compare.

#include <iostream>using namespace std;//Initializes an array of ordered BOOL Initarray (int *array, int arraySize) {for (int i = 0; I! =) ArraySize;    ++i) {Array[i] = i; } return true; recursive binary int binarysearchrecursion (int *array, int item, int begin, int end) {if (Begin > End) | |    Array = = NULL) {return-1;    } int middle = begin + (End-begin)/2;    if (array[middle] = = Item) {return middle;    } else if (item > Array[middle]) {return binarysearchrecursion (array,item,middle+1,end);    } else {return binarysearchrecursion (array,item,begin,middle-1); } return-1;}    Loop binary int binarysearchloop (int *array, int item, int begin, int end) {if (Array = = NULL) {return-1;        } while (begin <= end) {int middle = begin + (End-begin)/2;        if (array[middle] = = Item) {return middle;       } else if (item > Array[middle]) {begin = middle + 1; } else {end = middle-1; }} return-1;}    int main () {int arraySize = 100;    int *array = new Int[arraysize];    Initarray (array,arraysize);  /* In a binary lookup, end should point to the last element of the array, not to the next address of the last element, because the element is searched by the middle pointer that can be found by the binary, so the value passed in is arraySize-1 */cout    << binarysearchrecursion (array,-98,0,arraysize-1) << Endl;    cout << Binarysearchloop (array,-56,0,arraysize-1) << Endl; Delete Array;}


Binary lookup algorithm (recursive, loop)

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.