Binary Search Using Python

Source: Internet
Author: User
Binary Search Using Python
14:44:53 I am going to talk about buaa_shang
Add to favorites I want to contribute
The procedure is as follows:
[Python]
#! /Usr/bin/ENV Python
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:
Low = Mid + 1
Elif 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)
Run:
Administrator @ Ubuntu :~ /Python $ Python test_search2.py 123456789 4
3
Note:
1. '_': Because Python class members are both public and publicly accessible and public, there is a lack of private attributes like the Orthodox object-oriented language.
So we will use _ to simulate private attributes. These _ attributes are often used internally and generally do not need to be rewritten. No need to read.
The purpose of adding two underscores (_) is not to conflict with the names of common public properties, or to prevent users (non-developers) of objects from using them at will.
2. _ name _ = "_ main _" indicates that the script is directly executed.
If not, the script is imported by another program using import.
Related Article

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.