冒泡、二分、、、、

def bubble_sort(alist):
    '''
    冒泡排序
    '''
    n =len(alist)
    for j in range(n-1):
        #从上到下走多少次
        for i in range(n-1-j):
            #班长从头走到尾
            if alist[i]> alist[i+1]:
                alist[i],alist[i+1] = alist[i+1],alist[i]
时间复杂度O[n2] or O[n]
稳定性好,即在比较相同数据的时候,前边排布顺序不变化
def bubble_sort(alist):
    '''
    冒泡排序
    '''
    n =len(alist)
    for j in range(n-1):
        #从上到下走多少次
        count = 0
        for i in range(n-1-j):
            #班长从头走到尾
            if alist[i]> alist[i+1]:
                alist[i],alist[i+1] = alist[i+1],alist[i]
                count+=1
            if 0 == count:
                return


稳定性弱,
时间复杂度为O[n2],无法优化,最坏,最好
def select_sort(alist):

    n = len(alist)
    for j in range(n-1):
        min_index = j
        for i in range(min_index+1,n):
            if alist[min_index] > alist[i]:
                min_index = i
        alist[min_index],alist[j] = alist[j],alist[min_index]


最差时间复杂度为O[N2],最好为O[N]
稳定性好
def insert_sort(alist):

    n = len(alist)
    for j in range(1,n):
        i = j
        while i>0:
            if alist[i] < alist[i-1]:
                alist[i],alist[i-1] = alist[i-1],alist[i]
                i-=1
            else:
                break


时间复杂度为O[N2],稳定性差
def shell_sort(alist):
    n = len(alist)
    gap = n // 2
    while gap>0:
        for j in range(gap,n):
            i = j
            while i>0:
                if alist[i] < alist[i-gap]:
                    alist[i],alist[i-gap] = alist[i-gap],alist[i]
                    i -= gap
                else:
                    break
        gap //= 2


最优时间复杂度O[nlogn],最差O[n2],稳定性差
def quick_sort(slist,first,last):
    if first >= last:
        return
    middle_value = slist[first]
    low = first
    high = last
    while low < high:
        while low < high and slist[high] >= middle_value:
            high -= 1
        slist[low] = slist[high]
        while low < high and slist[low] < middle_value:
            low +=1
        slist[high] = slist[low]

    slist[low] = middle_value
    quick_sort(slist,first,low-1)
    quick_sort(slist,low+1,last)


def merge_sort(alist):
    """归并排序"""
    n = len(alist)
    if n <=1:
        return alist
    mid = n//2

    left_li = merge_sort(alist[:mid])
    right_li = merge_sort(alist[mid:])
    len_left = len(left_li)
    right_left = len(right_li)
    left_pointer,right_pointer = 0, 0
    result = []
    while left_pointer < len_left and right_pointer < right_left:
        if left_li[left_pointer] < right_li[right_pointer]:
            result.append(left_li[left_pointer])
            left_pointer+=1
        else:
            result.append(right_li[right_pointer])
            right_pointer+=1
    result += left_li[left_pointer:]
    result += right_li[right_pointer:]
    return result

def binary_search(alist,item):
    n = len(alist)
    if n > 0:
        mid = n //2
        if alist[mid] == item:
            return True
        elif item < alist[mid]:
            return binary_search(alist[:mid], item)
        else:
            return binary_search(alist[mid + 1:], item)
    return False

if __name__ == '__main__':
    li = [17,20,26,31,44,54,55,77,93]
    print( binary_search(li,55))
View Code