python 排序代码的实现

快速排序:

#!-*-coding:utf-8 -*-
#!@time :2018/9/11 16:06
#!@Author : shasha
#!@File : .py


def quick_sort(qlist):
    if qlist == []:
        return []
    else:
        qfirst = qlist[0]
        qless = quick_sort([l for l in qlist[1:] if l < qfirst])
        qmore = quick_sort([m for m in qlist[1:] if m >= qfirst])
        # print(qless + [qfirst] + qmore)
        return qless + [qfirst] + qmore


qlist = quick_sort([4, 5, 6, 7, 3, 2, 6, 9, 8])
print(qlist)

冒泡排序

#!-*-coding:utf-8 -*-
#!@time :2018/9/11 16:52
#!@Author : shasha
#!@File : .py


def bubble_sort(blist):
    plist = blist.copy()
    count = len(blist)
    for i in range(0, count - 1):
        if blist[i] > blist[i + 1]:
            blist[i], blist[i + 1] = blist[i + 1], blist[i]
        count = count - 1
    return plist, blist


lst = [0, 4, 5, 6, 7, 3, 2, 1]
if __name__ == '__main__':
    i = 0
    for i in range(len(lst)):
        blst, alst = bubble_sort(lst)
        if blst == alst:
            break
        print("第" + str(i+1) + "趟遍历的结果是:")
        print(blst)

插入排序:

#!-*-coding:utf-8 -*-
#!@time :2018/9/11 19:32
#!@Author : shasha
#!@File : .py

def insert_sort(lists):
    # 插入排序
    count = len(lists)
    for i in range(1, count):
        key = lists[i]
        j = i - 1
        while j >= 0:
            if lists[j] > key:
                lists[j + 1] = lists[j]
                lists[j] = key
            j -= 1
        print(lists)

    return lists


qlist = insert_sort([4, 5, 6, 7, 3, 2, 6, 9, 8])
print(qlist)

归并排序:

#!-*-coding:utf-8 -*-
#!@time :2018/9/11 20:17
#!@Author : shasha
#!@File : .py


def merge(a, b):
    c = []
    h = j = 0
    while j < len(a) and h < len(b):
        if a[j] < b[h]:
            c.append(a[j])
            j += 1
        else:
            c.append(b[h])
            h += 1

    if j == len(a):
        for i in b[h:]:
            c.append(i)
    else:
        for i in a[j:]:
            c.append(i)

    return c


def merge_sort(lists):
    if len(lists) <= 1:
        print(lists)
        return lists
    middle = len(lists)//2
    left = merge_sort(lists[:middle])
    right = merge_sort(lists[middle:])
    return merge(left, right)


if __name__ == '__main__':
    a = [4, 7, 8, 3, 5, 9]
    print(merge_sort(a))

堆排序:

#!-*-coding:utf-8 -*-
#!@time :2018/9/12 8:18
#!@Author : shasha
#!@File : .py
from collections import deque


def swap_param(L, i, j):
    L[i], L[j] = L[j], L[i]
    return L


def heap_adjust(L, start, end):
    tmp = L[start]

    i = start
    j = 2 * i

    while j < end:
        if (j < end) and (L[j] < L[j + 1]):
            j += 1
        if tmp < L[j]:
            L[i] = L[j]
            i = j
            j = 2 * i
        else:
            break
    L[i] = tmp


def heap_sort(L):
    L_length = len(L) - 1

    first_sort_count = L_length // 2

    for i in range(first_sort_count):
        heap_adjust(L, first_sort_count - i, L_length)
    # print([L[i] for i in range(1, len(L))])

    for i in range(L_length - 1):
        L = swap_param(L, 1, L_length - i)
        heap_adjust(L, 1, L_length - i - 1)
        # print([L[i] for i in range(1, len(L))])

    return [L[i] for i in range(1, len(L))]


if __name__ == '__main__':
    L = deque([50, 16, 30, 10, 60, 90, 2, 80, 70])
    L.appendleft(0)
    print(heap_sort(L))

猜你喜欢

转载自blog.csdn.net/weixin_38706928/article/details/82630743
今日推荐