Binary lookup (binary lookup)

Source: Internet
Author: User

Binary search criteria: ordered array.

Find the principle: The lookup process starts from the middle element of the array, and if the intermediate element is exactly equal to the element being searched, the search process ends;

If a particular element is greater than or less than the middle element, it is found in the half of the array greater than or less than the middle primary color, and is compared with the beginning from the middle element.

If an array of steps is empty, the representation cannot be found.

Each comparison of this search algorithm reduces the search scope by half.

Complexity of Time: O (LOGN)

There are two prerequisites for using two-point lookups:

1, the list you want to find must be ordered.

2, the data must be stored using the sequential storage structure of the linear table.

Here is the implementation code.

C # version:

Namespace binarysearch.csharp{class Program {static void Main (string[] args) {list<in            t> list = new list<int> {10,20,30,40,50,60,70,80,90};            Console.WriteLine ("******************** Two-point search ********************\n");            Display (list);            int result = BinarySearch (list, 40);            if (Result! =-1) Console.WriteLine ("40 in the list position is: {0}", result);            else Console.WriteLine ("Sorry, this element does not exist in the list!");        Console.readkey (); }///<summary>//<param name= "list" > lookup table </param >//<param name= "key" > Given value </param>//<returns> position of given value in the list </returns> publ            IC static int BinarySearch (list<int> List, int key) {int low = 0; int high = list.            Count-1;                while (low <= high) {int middle = (low + high)/2; Determine if intermediate records are associated with a given valueEqual if (list[middle] = = key) {return middle;  } else {//find if (List[middle] > key) high in the left half of the intermediate record                    = Middle-1;                Find else Low = middle + 1 in the right half of the middle record;        }}//Not found (failed to find) return-1; } private static void display (Ilist<int> list) {Console.WriteLine ("\n********** Show results *******            \ n "); if (list! = null && list. Count > 0) {foreach (var item in list) {Console.Write ("{0                } ", item);        }} Console.WriteLine ("\n********** Display completed **********\n"); }    }}

Program Output Results

Binary lookup (binary lookup)

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.