Binary search algorithm

Source: Internet
Author: User
def find (L, aim, start=0, End=none):
end = Len (l) If End is None else End # end = Len (l)
Mid_index = (End-start)//2 + start # Calculate intermediate values
If Start <= end:
If L[mid_index] < aim:
Return find (L, aim, Start=mid_index + 1, end=end) # Calculation
Elif L[mid_index] > aim:
Return find (L, aim, Start=start, end=mid_index-1)
Return Mid_index
Return "could not find this value"

L = [2, 3, 4, 5, 6, 7, 10, 12, 14, 15, 16, 17, 24, 25, 26, 29, 35, 36, 57, 60, 66, 86, 91, 93]

ret = Find (l, 39)
Print (ret)

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: 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.