首页 > 代码库 > python实现二分查找与冒泡排序

python实现二分查找与冒泡排序

二分查找,代码如下:

def binarySearch(l, t):
    low, high = 0, len(l) - 1
    while low < high:
        print low, high
        mid = (low + high) / 2
        if l[mid] > t:
            high = mid
        elif l[mid] < t:
            low = mid + 1
        else:
            return mid
    return low if l[low] == t else False

冒泡排序代码如下:

def bubble_sort(lists):

    # 冒泡排序

    count = len(lists)

    for i in range(0, count):

        for j in range(i + 1, count):

            if lists[i] > lists[j]:

                lists[i], lists[j] = lists[j], lists[i]

    return lists

 

python实现二分查找与冒泡排序