【Python】快速排序,归并排序,堆排序

目录

  • Java版
  • 快速排序
  • 归并排序
  • 堆排序

Java版

Java版代码:https://blog.csdn.net/m0_60370702/article/details/123417783?spm=1001.2014.3001.5501

快速排序

li = [i for i in range(1, 5000000)]
random.shuffle(li)

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)


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

        while left < right and li[left] <= tmp:
            left += 1
        li[right] = li[left]

    li[left] = tmp
    return left



quick_sort(li, 0, len(li) - 1)
print(li)

归并排序

li = [i for i in range(1, 5000000)]
random.shuffle(li)

def merge(li, low, mid, high):
    left_pointer = low  # 左侧列表第一个
    right_pointer = mid + 1  # 右侧列表第一个
    li_tmp = []
    while left_pointer <= mid and right_pointer <= high:  # 左右两边都有数
        if li[left_pointer] < li[right_pointer]:
            li_tmp.append(li[left_pointer])
            left_pointer += 1
        else:
            li_tmp.append(li[right_pointer])
            right_pointer += 1
    # while 执行完,必有一部分没有数
    while left_pointer <= mid:
        li_tmp.append(li[left_pointer])
        left_pointer = + 1
    while right_pointer <= high:
        li_tmp.append(li[right_pointer])
        right_pointer += 1
    li[low:high + 1] = li_tmp



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



li2 = merge_sort1(li, 0, len(li)-1)
print(li2)
li = [i for i in range(1, 5000000)]
random.shuffle(li)


def merge_sort(li):
    n = len(li)
    if n <= 1:
        return li
    mid = n // 2
    li_left = merge_sort(li[:mid])
    li_right = merge_sort(li[mid:])
    result_li = []
    left_pointer = 0
    right_pointer = 0
    while left_pointer < len(li_left) and right_pointer < len(li_right):
        if li_left[left_pointer] < li_right[right_pointer]:
            result_li.append(li_left[left_pointer])
            left_pointer += 1
        else:
            result_li.append(li_right[right_pointer])
            right_pointer += 1
    result_li += li_left[left_pointer:]
    result_li += li_right[right_pointer:]
    return result_li



li = merge_sort(li1)
print(li)

堆排序

li = [i for i in range(1, 5000000)]
random.shuffle(li)

def sift(li, low, high):
    """
    @param li: 列表
    @param low: 堆的根节点位置
    @param high: 堆的最后一个元素位置
    @return:
    """
    i = low  # 根节点
    j = 2 * i + 1  # 子节点
    tmp = li[low]  # 堆顶存起来
    while j <= high:  # 只要j位有数
        if j + 1 <= high and li[j + 1] > li[j]:  # 如果右孩子,且比较大
            j = j + 1  # 指向右孩子
        if li[j] > tmp:
            li[i] = li[j]
            i = j  # 往下一层
            j = 2 * i + 1
        else:  # tmp 更大,把 tmp 放到 i 上
            # 把tmp放到某一级领导位置上
            break
    li[i] = tmp  # 把tmp放到叶子节点上


def heap_sort(li):
    n = len(li)
    for i in range(n // 2 - 1, -1, -1):
        # i 代表建堆时调整的部分的根的下标
        sift(li, i, n - 1)
    # 建堆完成
    for i in range(n - 1, -1, -1):
        # 指向当前堆的最后一个元素
        li[0], li[i] = li[i], li[0]
        sift(li, 0, i - 1)  # i-1时是新的high



heap_sort(li)
print(li)

import heapq
import random

lis = list(range(30))
random.shuffle(lis)  # 打乱
print(lis)
heapq.heapify(lis)  # 建堆
n = len(lis)
for i in range(n):
    print(heapq.heappop(lis), end=',')

你可能感兴趣的:(leetcode刷题总结,python,排序算法)