python實現常見的五種排序演算法

Geekrun發表於2019-02-16

概要

演算法理論講解有專業的書籍和視訊資源,本篇文章主要展示演算法排序的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,演算法(普林斯頓大學)

相關文章