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

时间:2024-04-02 14:07:32

二分查找,代码如下:

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