二分法查找的Python实现

代码如下:

#!/usr/bin/env python
# coding=utf-8

def BinarySearch(t,x):
    t.sort() #对列表进行排序,列表是有序的,是二分法的前提
    low = 0;
    high = len(t)-1;
    while low < high:
        mid = (low+high)/2;
        if t[mid]x:
            high = mid-1;
        else :
            return mid
    return Non


你可能感兴趣的:(Python)