python实现常见的五种排序算法

概要

算法理论讲解有专业的书籍和视频资源,本篇文章主要展示算法排序的python语言描述,具体讲解的资源地址参见文末参考引用

冒泡排序(Bubble Sort)

# 冒泡排序
def bubbleSort(seq=None, reversed=False):
    lens = len(seq)
    for i in range(lens):
        for j in range(lens - i - 1):
            if (seq[j] < seq[j + 1] if reversed else   seq[i] > seq[j]):
                seq[j], seq[j + 1] = seq[j + 1], seq[j]
    return seq

if __name__=="__main__":
    #打印结果为:[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    print(bubbleSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13],True))

选择排序(Selection Sort)

# 选择排序
def selectionSort(seq=None, reversed=False):
    lens = len(seq)
    for i in range(lens):
        min_index = i
        for j in range(i + 1, lens):
            if (seq[min_index] < seq[j] if reversed else   seq[i] > seq[j]):
                min_index = j
        seq[i], seq[min_index] = seq[min_index], seq[i]
    
    return seq


if __name__ == "__main__":
    # 打印结果为:[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    print(selectionSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13], True))

插入排序(Insertion Sort)

# 插入排序
def insertionSort(seq=None, reversed=False):
    lens = len(seq)
    for i in range(1, lens):
        key = seq[i]
        j = i
        while j > 0 and (seq[j - 1] < seq[j] if reversed else    seq[j - 1] > seq[j]):
            seq[j], seq[j - 1] = seq[j - 1], seq[j]
            j -= 1
    
    return seq


if __name__ == "__main__":
    # 打印结果为:[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    print(insertionSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13], True))

归并排序(Selection Sort)

# 归并排序(分)
def mergeSort(seq):
    if len(seq) < 2:
        return seq
    mid = len(seq) // 2
    left = mergeSort(seq[:mid])
    right = mergeSort(seq[mid:])
    return merge(left, right)


# 归并排序(治)
def merge(left, right):
    if not len(left) or not len(right):
        return left or right
    result = []
    i, j = 0, 0
    
    while (len(result) < len(left) + len(right)):
        if left[i] < right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
        if i == len(left) or j == len(right):
            result.extend(left[i:] or right[j:])
            break
    return result


if __name__ == "__main__":
    # 打印结果为:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    print(mergeSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13]))

快速排序(Selection Sort)

# 快速排序
def quickSort(seq, start, end):
    if start < end:
        split = partition(seq, start, end)
        quickSort(seq, start, split - 1)
        quickSort(seq, split + 1, end)
    return seq


def partition(seq, start, end):
    pivot_index = start - 1
    for i in range(start, end):
        # 选择最右边的为pivot
        if seq[i] < seq[end]:
            pivot_index += 1
            seq[pivot_index], seq[i] = seq[i], seq[pivot_index]
    seq[end], seq[pivot_index + 1] = seq[pivot_index + 1], seq[end]
    return pivot_index + 1


if __name__ == "__main__":
    # 打印结果为:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    print(quickSort([10, 1, 3, 5, 7, 9, 2, 4, 6, 8, 11, 15, 0, 12, 14, 13], 0, 15))

参考引用

1, sole learn,ios、android均可免费下载
2, github源文件地址
3,北大公开课 算法设计与分析 屈婉玲教授
4,数据结构-浙江大学
5,算法(普林斯顿大学)

    原文作者:Geekrun
    原文地址: https://segmentfault.com/a/1190000013543101
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞