Python for two-point lookup

Source: Internet
Author: User

Binary search: Find in a set of ordered data, first compared with the middle number first, if the number of lookups is greater than the middle number, then to find the number in the first half, or in the second half, so that will exclude half of the data, so recursively go on, it will greatly improve the speed of the search. The following two-point search using Python code

defBinary_search (Array,key): Low=0 High= Len (array)-1 whileLow <High:mid= Int (low + high)/2)        ifKey <Array[mid]: high= Mid-1elifKey >Array[mid]: Low= Mid + 1Else:            returnMidreturnFalsedefMain (): Array= [1,2,3,4,5,6,7,8,9,10,14,15,17,21,24,28,35,35,37,39] Result= Binary_search (array,7)    Print(Result)if __name__=='__main__': Main ()

After sorting returns the index of the data being looked up in the list, and returns False if no search is found

Python for two-point 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.