Python 实现十大排序算法

冒泡排序

1.代码

def bubble_sort(arr):
    for i in range(len(arr) - 1):
        for j in range(len(arr) - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

2.改进

def bubble_sort(arr):
    for i in range(len(arr) - 1):
        ex = True
        for j in range(len(arr) - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                ex = False
        if ex:
            return

选择排序

def selection_sort(arr):
    for i in range(len(arr)):
        min_loc = i
        for j in range(i + 1, len(arr)):
            if arr[j] < arr[min_loc]:
                min_loc = j
        arr[min_loc], arr[i] = arr[i], arr[min_loc]

插入排序

def insert_sort(arr):
    for i in range(1, len(arr)):
        temp = arr[i]
        j = i - 1
        while temp < arr[j] and j >= 0:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = temp

快速排序

def partition(arr, left, right) -> int:
    temp = arr[left]
    while left < right:
        while left < right and temp <= arr[right]:
            right -= 1
        arr[left] = arr[right]
        while left < right and temp >= arr[left]:
            left += 1
        arr[right] = arr[left]
    arr[left] = temp
    return left


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

堆排序

1.代码

def shift(arr, low, high):  # 用于将根结点调整到正确的位置
    i = low
    j = 2 * i + 1
    temp = arr[low]
    while j <= high:
        if j + 1 <= high and arr[j + 1] > arr[j]:
            j += 1
        if arr[j] > temp:
            arr[i] = arr[j]
            i = j
            j = 2 * i + 1
        else:
            break
    arr[i] = temp


def heap_sort(arr):
    n = len(arr)
    for i in range((n - 2) // 2, -1, -1):
        shift(arr, i, n - 1)
    # 建堆完成,接下来逐个出数
    for i in range(n - 1, -1, -1):  # i指向堆的最后一个元素,每次将堆最大的一个元素写到列表的末尾
        arr[0], arr[i] = arr[i], arr[0]
        shift(arr, 0, i - 1)    # i - 1指向下一个堆的最后一个元素

2.使用python内置模块实现

import random
import heapq
lis = [random.randint(0, 10) for i in range(10)]    # 生成一个随机数列表
print(lis)
heapq.heapify(lis)  # 构造一个小根堆
for i in range(len(lis) - 1):
    num = heapq.heappop(lis)    # 每次弹出最小的数
    print(num, end=' ')         # 打印每次弹出的数

归并排序

def merge(arr, low, mid, high):
    i = low
    j = mid + 1
    temp = []
    while i <= mid and j <= high:
        if arr[i] > arr[j]:
            temp.append(arr[i])
            i += 1
        else:
            temp.append(arr[j])
            j += 1
    while i <= mid:
        temp.append(arr[i])
        i += 1
    while j <= high:
        temp.append(arr[j])
        j += 1
    arr[low: high + 1] = temp


def merge_sort(arr, low, high):
    if low < high:
        mid = (low + high) // 2
        merge_sort(arr, low, mid)
        merge_sort(arr, mid + 1, high)
        merge(arr, low, mid, high)

希尔排序

def insert_sort_gap(arr, gap):
    for i in range(gap, len(arr)):
        temp = arr[i]
        j = i - gap
        while j >= 0 and arr[j] > temp:
            arr[j + gap] = arr[j]
            j -= gap
        arr[j + gap] = temp


def shell_sort(arr):
    d = len(arr) // 2
    while d >= 1:
        insert_sort_gap(arr, d)
        d //= 2

计数排序

def count_sort(arr, max_count):
    count = [0 for _ in range(max_count + 1)]
    for val in arr:
        count[val] += 1
    arr.clear()
    for ind, val in enumerate(count):
        for i in range(val):
            arr.append(ind)

桶排序

def bucket_sort(arr, n, max_num):
    buckets = [[] for _ in range(n)]     # 创建桶
    for var in arr:
        i = min(var // (max_num // n), n - 1)   # 确定var应该位于哪一个桶里
        # 取min是为了防止数组越界,如var取到max_num且可以被n整除
        buckets[i].append(var)  # 将var加入桶
        for j in range(len(buckets[i]) - 1, 0, -1):     # 维持每个桶有序,可以永其他的排序方法
            if buckets[i][j] < buckets[i][j - 1]:
                buckets[i][j], buckets[i][j - 1] = buckets[i][j - 1], buckets[i][j]
            else:
                break
    ret = []
    for buc in buckets:
        ret.extend(buc)
    return ret

基数排序

def radix_sort(arr):
    max_num = max(arr)
    it = 0
    while 10 ** it <= max_num:
        buckets = [[] for _ in  range(10)]
        for var in arr:
            digit = (var // 10 ** it) % 10
            buckets[digit].append(var)
        arr.clear()
        for buc in buckets:
            arr.extend(buc)
        it += 1
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值