数据结构之二分查找

47 阅读1分钟
#!/usr/bin/env python2.7
# -*- conding: utf-8 -*-
# @Time : 2020/9/15 19:36
# @Author : henry
# @file : binarySearch.py.py
# @Project: python-script 


def binarySerach(datalist,item):
    found = False
    low = 0
    high = len(datalist) - 1
    while low <= high and not found:
        mid = (low + high)/2
        if datalist[mid] == item:
            found = True
            return mid
        else:
            if datalist[mid] > item:
                high = mid - 1
            else:
                low = mid + 1

    return found

datalist = [3,18,20,25,34,42,48,53]
print(binarySerach(datalist,42))