八大排序算法 python实现

# 直接插入排序  时间复杂度:O(n2)  稳定
def insert_sort(array):
    for i in range(len(array)):
        for j in range(i):
            if array[i] < array[j]:
                array.insert(j, array.pop(i))
                break
    return array


# 希尔排序 时间复杂度:O(n**1.3) 不稳定
def shell_sort(array):
    gap = len(array)
    while gap > 1:
        gap = gap // 2
        for i in range(gap, len(array)):
            for j in range(i % gap, i, gap):
                if array[i] < array[j]:
                    array[i], array[j] = array[j], array[i]
    return array


# 直接选择排序 时间复杂度:O(n2) 不稳定
def select_sort(array):  # 最小的数往前面放,与冒泡排序类似相反,但不稳定
    for i in range(len(array)):
        x = i  # min index
        for j in range(i, len(array)):
            if array[j] < array[x]:
                x = j
        array[i], array[x] = array[x], array[i]
    return array


# 堆排序 时间复杂度:O(nlogn) 不稳定
def heap_sort(array):
    def heap_adjust(parent):
        child = 2 * parent + 1  # left child
        while child < len(heap):
            if child + 1 < len(heap) and heap[child] < heap[child + 1]:
                child += 1  # right child
            if heap[parent] >= heap[child]:
                break
            heap[parent], heap[child] = heap[child], heap[parent]
            parent, child = child, 2 * child + 1  # 向下更新

    heap, array = array[:], []
    for i in range(len(heap) // 2 - 1, -1, -1):  # 从下向上更新大根堆
        heap_adjust(i)
    while len(heap) != 0:
        heap[0], heap[-1] = heap[-1], heap[0]
        array.insert(0, heap.pop())
        heap_adjust(0)  # 从下向上更新
    return array


# 冒泡排序 时间复杂度:O(n2),最快为n,  稳定
def bubble_sort(array):  # 最大往后面冒泡,稳定,最优情况下复杂度为O(n),其他为O(n2)
    for i in range(len(array) - 1):
        flag = False
        for j in range(len(array) - 1 - i):
            if array[j] > array[j + 1]:
                array[j], array[j + 1] = array[j + 1], array[j]
                flag = True
        if flag == False:
            return
    return array


# 快速排序 时间复杂度:O(nlogn) 不稳定
def quick_sort(array):  # 二分,时间复杂度为:nlogn,不稳定
    def recursive(begin, end):
        if begin >= end:
            return
        l, r = begin, end
        flag = array[begin]
        while l < r:
            while l < r and array[r] > flag:
                r -= 1
            while l < r and array[l] <= flag:
                l += 1
            array[l], array[r] = array[r], array[l]
        array[begin], array[l] = array[l], array[begin]
        recursive(begin, l - 1)
        recursive(r + 1, end)

    recursive(0, len(array) - 1)
    return array


# 归并排序 时间复杂度:O(nlogn) 稳定
def merge_sort(array):  # 归并排序,复杂度为:nlogn, 稳定,开辟新数组存储,原数组不变
    def merge_arr(arr_l, arr_r):
        array = []
        while len(arr_l) and len(arr_r):
            if arr_l[0] <= arr_r[0]:
                array.append(arr_l.pop(0))
            else:
                array.append(arr_r.pop(0))
        if len(arr_l) != 0:
            array += arr_l
        elif len(arr_r) != 0:
            array += arr_r
        return array

    def recursive(array):
        if len(array) == 1:
            return array
        mid = len(array) // 2
        arr_l = recursive(array[:mid])
        arr_r = recursive(array[mid:])
        return merge_arr(arr_l, arr_r)

    return recursive(array)


# 基数排序 时间复杂度:O(d(r+n)),d代表最大位数,r代表桶个数,n代表数组长度,   稳定
def radix_sort(array):
    bucket, digit = [[]], 0
    while len(bucket[0]) != len(array):
        bucket = [[], [], [], [], [], [], [], [], [], []]
        for i in range(len(array)):
            num = (array[i] // 10 ** digit) % 10  # 依次散列个位数,十位数...
            bucket[num].append(array[i])
        array.clear()
        for i in range(len(bucket)):
            array += bucket[i]
        digit += 1
    return array

 

你可能感兴趣的:(数据结构与算法,数据结构与算法)