In this paper, we describe the method of realizing binary search algorithm in Python. Share to everyone for your reference. The implementation method is as follows:
#!/usr/bin/env pythonimport sys def SEARCH2 (a,m): Low = 0 High = Len (a)-1 while (Low <= High): mid = ( Low + high)/2 midval = A[mid] if midval < m: Low = mid + 1 elif midval > M: High = Mid-1 E LSE: Print mid return mid print-1 return-1if __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 public and publicly accessible public, there is a lack of proprietary private properties like the Orthodox object-oriented language.
So just use __ to simulate the private properties. These __ properties are often used internally and are usually not rewritten. And not read.
Add 2 underline purpose, one is not and common public attribute duplicate name conflict, second, not let the object user (non-developer) arbitrary use.
2.__name__ = = "__main__" indicates that the program script is executed directly.
If not equal to means that the script was introduced with import by another program. Its __name__ property is set to the module name
Hopefully this article will help you with Python programming.