Binary search algorithm

Source: Internet
Author: User

-IntroductionBinary search also known as binary lookup, the advantages are less than the number of comparisons, Find Fast, average performance, occupy less system memory, 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.
-Thought-Two points the lookup is searched from the middle in each search, provided that the array or list to be looked up must be sorted in order. such as the number of rows from 1-100, I would like to think of a number, you guess, when you guess the number is larger than mine, I will say ' big ', you will go to the ' small ' side to start guessing, when you guess the number is smaller than mine, I will say ' small ', you have to ' big ' side to guess. So, every time you start guessing from the middle, guess the least number of times.-Summary:The basic idea of binary search is to divide n elements into roughly equal two parts, take A[N/2] and X to compare, if X=A[N/2], then find X, the algorithm aborts, if X<A[N/2], as long as the left half of the array a continues to search X, if X>A[N/2], Search for x in the right half of the array A.The complexity of time is nothing more than the number of while loops! There are a total of n elements,gradually followed is N,N/2,N/4,.... n/2^k (the remainder of the operation Element), where k is the number of cyclesbecause you n/2^k take the whole post >=1even if n/2^k=1can be k=log2n, (is the logarithm of the base of 2, N)so time complexity can represent O (h) =o (log2n) -Algorithm Requirements
    1. Sequential storage structures must be used.
2. Must be sorted by keyword size.
-run Time-When you start guessing from 1, you need to guess up to 100 times, i.e. ' O (n) ' linear time; -when guessing with a dichotomy, half of each guess is divided by 2, except that it is rounded up until the remaining number is the maximum number of times you can guess. ' 100/2/2/2/2/2/2/2=1 ' except 2 times as many times as you guessed. This number is a logarithm with a low of 2 to 100: ' ㏒?10 ' logarithmic time;-it takes full advantage of the order relationship between elements and uses a divide-and-conquer strategy to complete the search task in the worst case with O (log n). Its basic idea is that n elements are divided into roughly the same number of two halves, take A[N/2] and to find the X to compare, if X=A[N/2] Find x, the algorithm terminates. If X&LT;A[N/2], we simply continue to search for x in the left half of array a (assuming that the array elements are in ascending order). If X&GT;A[N/2], then we just continue to search for x in the right half of array A.


-Algorithm Code (PYTHON3)"' Pythondef binary_search (list, item): low = 0 High = len (list)-1 when low <= High:mid = (lo            W + high)//2 guess = List[mid] if guess = = Item:return Mid If guess < item: Low = mid + 1 Else:high = mid-1 return None
MyList = [1,4,6,8,9]
Print (Binary_search (mylist,10)) print (Binary_search (mylist,6))
```

Binary search 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.