Binary lookup/binary lookup algorithm

Source: Internet
Author: User

Binary search also known as binary lookup, the advantages are less than the number of comparisons, Find Fast, the average performance is good, the disadvantage is that the unknown origin table is ordered table, and insert delete difficult. Therefore, the binary lookup method is suitable for an ordered list that does not change frequently and finds frequent. First, suppose that the elements in the table are arranged in ascending order, comparing the keywords in the middle position of the table with the lookup keywords, and if they are equal, the lookup succeeds; otherwise, the table is divided into the front and the last two sub-tables with the intermediate positional records, and if the middle position record keyword is greater than the Find keyword, the previous child Otherwise, find the latter child table further. Repeat the process until you find a record that satisfies the criteria, make the lookup successful, or until the child table does not exist, the lookup is unsuccessful at this time.

classprogram{Static voidMain (string[] args) {        int[] Array =New[] { at,98,234,765,974,867,86786,145432,8676343,9999999};//Target Array        intFindvalue =145432;//is searched for numberConsole.WriteLine (BinarySearch (Array, findvalue,0, array. Length-1) ?"the number of lookups is present in the array":"the number of lookups is not present in the array");    Console.readkey (); }        /// <summary>    ///binary Lookup/binary lookup (divide-and-conquer thought, recursive, target array must be ordered), algorithm complexity is O (log (n), n is the target number of leader degree)/// </summary>    /// <param name= "Sources" >Target Array</param>    /// <param name= "Findvalue" >number of target lookups</param>    /// <param name= "Low" >Interval Minimum index</param>    /// <param name= "High" >Interval Maximum index</param>    /// <returns>true: exists, false, does not exist</returns>    Private Static BOOLBinarySearch (int[] Sources,intFindvalue,intLowintHigh ) {        //not found, terminating recursion        if(Low > High)return false; //binary Find Median index: (A + B)/2 means mean arithmetic, midpoint        intMiddleindex = (low + high)%2==0? (Low + high)/2: (Low + high)/2+1; if(Findvalue >Sources[middleindex]) {            //greater than middle value, in interval [Middleindex + 1, high] recursion continues to find            returnBinarySearch (sources, findvalue, Middleindex +1, high); }        if(Findvalue <Sources[middleindex]) {            //less than middle value, in interval [low, middleIndex-1] recursion continues to find            returnBinarySearch (Sources, findvalue, low, Middleindex-1); }        //findvalue equals Sources[middleindex], found, terminating recursion        return true; }}

Binary lookup/binary lookup algorithm

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.