令牌桶算法和漏桶算法python_Python 排序算法

参考:https://github.com/jackfrued/Python-100-Days/blob/master/Day16-20

数据结构和算法

算法:解决问题的方法和步骤

评价算法的好坏:渐近时间复杂度和渐进空间复杂度

渐近时间复杂度的大o标记:

O(c)-常量时间复杂度-布隆过滤器/哈希存储

O(log2n : 注 :‘2’为底数)-对数时间复杂度-折半查找(二分查找)

O(n)-线性时间复杂度-顺序查找/桶排序

O(n*log2n : 注 :‘2’为底数)-对数线性时间复杂度-高级排序算法(归并排序/快速排序)

O(n^2)-平方时间复杂度-简单排序算法(选择排序、插入排序、冒泡排序)

O(n^3)-立方时间复杂度-Floyd算法/矩阵乘法运算

O(2^n)-几何级数时间复杂度-汉诺塔

O(n!)-阶乘时间复杂度-旅行经销商问题-NP

f76b0a9bd3e3d954d826961499e6424b.png
引用参考资料图片

排序算法(选择、冒泡和归并)和查找算法(顺序和折半)

简单选择排序

"""简单选择排序"""
def select_sort(origin_items, comp=lambda x, y: x < y):
    items = origin_items
    for i in range(len(items) - 1):
        min_index = i
        for j in range(i + 1, len(items)):
            if comp(items[j], items[min_index]):
                min_index = j
        items[i], items[min_index] = items[min_index], items[i]
    return items

def main():
    list1 = [3, 1, 6, 2]
    print(select_sort(list1))

if __name__ == '__main__':
    main()

插入排序

"""插入排序"""
def insert_sort(items):

    for i in range(1, len(items)):
        key = items[i]
        j = i - 1
        while j >= 0 and key < items[j]:
            items[j + 1] = items[j]
            j -= 1
        items[j + 1] = key
    return items

def main():
    list1 = [5, 11, 7, 10, 13, 6]
    print(insert_sort(list1))

if __name__ == '__main__':
    main()

冒泡排序

"""冒泡排序"""
def bubble_sort(origin_items, comp=lambda x, y: x > y ):
    items = origin_items[:]
    for i in range(len(items) - 1):
        flag = 1
        for j in range(len(items)- 1 - i):
            if comp(items[j], items[j + 1]):
                items[j], items[j + 1] = items[j + 1], items[j]
                flag = 0
        if flag:
            break
    return items
def main():
    list1 = [6, 1, 2, 3]
    print(bubble_sort(list1))
    
if __name__ == '__main__':
    main()
            


"""高质量冒泡排序(搅拌排序)"""
def bubble_sort(origin_items, comp=lambda x, y: x > y):
    items = origin_items[:]
    for i in range(len(items) - 1):
        swapped = False
        #将大值放于右边
        for j in range(i, len(items) - 1 - i):
            if comp(items[j], items[j + 1]):
                items[j], items[j + 1] = items[j + 1], items[j]
                swapped = True
        # 将小值放于左边
        if swapped:
            swapped = False
            for j in range(len(items) - 2 - i, i, -1):
                if comp(items[j - 1], items[j]):
                    items[j - 1], items[j] = items[j], items[j - 1]
                    swapped = True
        #跳出循环
        if not swapped:
            break
    return items


def main():
    list1 = [6, 3, 2, 1]
    print(bubble_sort(list1))

if __name__ == '__main__':
    main()

归并排序

"""归并排序(分治法)"""
def merge_sort(items):
    if len(items) < 2:
        return items
    mid = len(items) // 2
    left = merge_sort(items[:mid])
    right = merge_sort(items[mid:])
    return merge(left, right)

def merge(items1, items2, comp=lambda x, y: x <= y):
    """合并(将两个有序的列表合并成一个有序的列表)"""
    items = []
    index1, index2 = 0, 0
    while index1 < len(items1 ) and index2 < len(items2):
        if comp(items1[index1], items2[index2]):
            items.append(items1[index1])
            index1 += 1
        else:
            items.append(items2[index2])
            index2 += 1
    items += items1[index1:]
    items += items2[index2:]
    return items

def main():
    list1 = [3, 1, 6, 2, 7, 9, 4, 2]
    print(merge_sort(list1))

if __name__ == '__main__':
    main()

快速排序

# def partition(items, start, end, comp=lambda x, y: x <= y):
def partition(items, start, end):
    """划分"""
    pivot = items[end]
    i = start - 1
    for j in range(start, end):
        if items[j] <= pivot:
        # if comp(items[j], pivot):
            i += 1
            items[i], items[j] = items[j], items[i]
    items[i + 1], items[end] = items[end], items[i + 1]
    return (i + 1)

def quick_sort(items,start, end):
    print('items', items)
    if start < end:
        pos = partition(items, start, end)

        quick_sort(items, start, pos - 1)
        quick_sort(items, pos + 1, end)
    return items
def main():
    # list1 = [4, 2, 5]
    list1 = [4, 2, 7, 6, 1, 5]
    print(quick_sort(list1, 0, len(list1) - 1))

if __name__ == '__main__':
    main()

顺序查找

"""顺序查找"""
def seq_search(items, key):
    for index, item in enumerate(items):
        if item == key:
            return index
    return -1

def main():
    list1 = [3, 1, 6, 2, 7, 9, 4, 2]
    key = int(input('input the key:'))
    print(seq_search(list1, key))

if __name__ == '__main__':
    main()

折半查找

"""折半查找"""
def bin_search(items, key):
    start, end = 0, len(items) - 1
    while start <= end:
        mid = (start + end) // 2
        if key > items[mid]:
            start = mid + 1
        elif key < items[mid]:
            end = mid - 1
        else:
            return mid

def main():
    list1 = [3, 5, 6, 7, 9, 10, 12]
    key = int(input('input the key:'))
    print(bin_search(list1, key))

if __name__ == '__main__':
    main()
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页