python实现冒泡排序、快速排序,以及执行效率对比

冒泡排序

import time
import random


def bubble_sort(li):
    """冒泡排序"""
    length = len(li)
    for x in range(0, length - 1):
        for y in range(x + 1, length):
            if li[x] > li[y]:
                li[x], li[y] = li[y], li[x]


if __name__ == '__main__':
    start_time = time.time()
    li = [i for i in range(10000)]
    random.shuffle(li)
    bubble_sort(li)
    print(time.time() - start_time)  # 耗时:9s左右
    print(li[:100])

windows2016款机器耗时:7-9秒左右,mac m1耗时:3秒左右。

快速排序-方法1

import random
import time


def partition(li, left, right):
    while left < right:
        while left < right and li[left] <= li[right]:
            left += 1
        li[left], li[right] = li[right], li[left]

        while left < right and li[right] >= li[left]:
            right -= 1
        li[right], li[left] = li[left], li[right]

    return left


def quick_sort(li, left, right):
    if left < right:
        mid = partition(li, left, right)
        quick_sort(li, left, mid - 1)
        quick_sort(li, mid + 1, right)


if __name__ == '__main__':
    start_time = time.time()
    li = [i for i in range(10000)]
    random.shuffle(li)
    quick_sort(li, 0, len(li) - 1)
    print(time.time() - start_time)  # 耗时60ms左右
    print(li[:100])

windows2016款机器耗时:60-70毫秒左右。

你的机器耗时多少秒呢?来评论看看

快速排序-方法2

import random
import time


def partition(li, left, right):
    tmp = li[left]
    while left < right:
        while left < right and li[right] >= tmp:  # 从右边找比tmp小的数
            right -= 1
        li[left] = li[right]

        while left < right and li[left] <= tmp:  # 从左边找比tmp大的数
            left += 1
        li[right] = li[left]

    li[left] = tmp

    return left


def quick_sort(li, left, right):
    if left < right:
        mid = partition(li, left, right)
        quick_sort(li, left, mid - 1)
        quick_sort(li, mid + 1, right)


if __name__ == '__main__':
    start_time = time.time()
    li = [i for i in range(1000000)]
    random.shuffle(li)
    quick_sort(li, 0, len(li) - 1)
    print(time.time() - start_time)
    print(li[:100])

非递归方法实现快速排序

import time
import random
import collections


def partition(li, left, right):
    tmp = li[left]
    while left < right:
        while left < right and li[right] >= tmp:  # 从右边找比tmp小的数
            right -= 1
        li[left] = li[right]
        while left < right and li[left] <= tmp:  # 从左边找比tmp大的数
            left += 1
        li[right] = li[left]
    li[left] = tmp
    return left


def quick_sort(arr, left, right):
    stack = collections.deque()
    stack.append(left)
    stack.append(right)
    while len(stack) > 0:
        right = stack.pop()
        left = stack.pop()
        if left < right:
            mid = partition(arr, left, right)
            stack.append(left)
            stack.append(mid - 1)
            stack.append(mid + 1)
            stack.append(right)


if __name__ == '__main__':
    list1 = [i for i in range(1000000)]
    random.shuffle(list1)
    print(list1[:100])
    start = time.time()
    quick_sort(list1, 0, len(list1) - 1)
    print(time.time() - start)

    print(list1[:100])

posted @ 2022-06-14 10:47  专职  阅读(97)  评论(0编辑  收藏  举报