Python-基础算法之二分查找

 

Python-基础算法之二分查找

def binary_search(data_source,find_n):
    mid = int(len(data_source)/2)
    if len(data_source)>1:
        if data_source[mid] >= find_n : #date in left
            print("data in left of [%s]" %data_source[mid])
            binary_search(data_source[:mid],find_n)
        elif    data_source[mid] < find_n : #date in right
                print("data in right of [%s]" %data_source[mid])
                binary_search(data_source[mid:],find_n)
        else:
            print("found finds,",data_source[mid])
if  __name__ == '__main__':
    data = list(range(1,10000))
    print(data)
binary_search(data,22)



/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day3/二分法.py
data in left of [5000]
data in left of [2500]
data in left of [1250]
data in left of [625]
data in left of [313]
data in left of [157]
data in left of [79]
data in left of [40]
data in right of [20]
data in left of [30]
data in left of [25]
found finds, 22

 

posted on 2017-08-31 10:56  光阴8023  阅读(95)  评论(0编辑  收藏  举报