An example of binary lookup algorithm in Python

Source: Internet
Author: User
Tags in python

In this paper, we illustrate the method of Python's binary lookup algorithm. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 #!/usr/bin/env python import sys def SEARCH2 (a,m): low = 0 High = Len (a)-1 while (Low <= high): Mid = (low + high)/2 m Idval = A[mid] If midval < M:low = mid + 1 elif midval > M:high = Mid-1 else:print mid return mid print-1 Retu rn-1 if __name__ = = "__main__": a = [int (i) for I in List (sys.argv[1])] m = Int (sys.argv[2)) SEARCH2 (a,m)

Run:

administrator@ubuntu:~/python$ Python test_search2.py 123456789 4

3

Note:

1. ' __ ': Because Python's class members are publicly and publicly accessible, there is a lack of proprietary private private properties like the Orthodox object-oriented language.

So just use __ to do it, and simulate the private property. These __ attributes are often used internally and are usually not rewritten. Also do not have to read.

Plus 2 underline the purpose, one is not the same as common public attributes conflict, the second is not to let the object of the user (not developers) at will use.

2.__name__ = = "__main__" indicates that the program script is executed directly.

If it is not equal to indicate that the script was introduced by another program with import. Then its __name__ property is set to the module name

I hope this article will help you with your Python programming.

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.