Python binary search

Source: Internet
Author: User
The following is a binary search code implemented by Python. The following is a binary search code implemented by Python.

# Encoding = UTF-8 import sys def search2 (a, m): low = 0 high = len (a)-1 while low <= high: mid = (low + high) /2 midval = a [mid] if midval

 
  
M: high = mid-1 else: print mid return mid print-1 return-1 if _ name _ = "_ main __": a = [int (I) for I in list (sys. argv [1])] m = int (sys. argv [2]) search2 (a, m)

 

Test result:

Shao @ ubuntu :~ /Tmp $ python test_search2.py 123456789 4 3


For more articles about python binary search, refer to the Chinese PHP website!

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.