python--数据结构--冒泡排序、快速排序

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码实现:

# change_type_sort.py
from collections import deque
from typing import List


class Record:
    def __init__(self, key):
        self.key = key
        self.other_info = None


class Records:
    def __init__(self, record_list: List[Record]):
        self.record_list = deque([None])
        self.record_list.extend(record_list)
        self.record_len = len(self.record_list)-1
        self.other_info = None


def bubble_sort(records: Records):
    """
    冒泡排序:此处升序排序

    :param records: 待排序的记录集对象
    :return: 排好序的记录集对象
    """
    i = records.record_len
    change = True  # change的作用为了当某趟排序完成后已实现记录集的排序,避免后序的多趟排序,可以减少不必要的比较和交换
    while change and i > 1:
        # 升序排序,每趟冒一个最大值,并将其放在第i个位置,因此i是递减的,并大于1
        change = False
        for j in range(1, i):
            # 第i趟相当于在i个记录中找一个记录关键字最大值,需进行i-1次比较。
            if records.record_list[j].key > records.record_list[j+1].key:
                records.record_list[j], records.record_list[j+1] = records.record_list[j+1], records.record_list[j]
                change = True
        i -= 1  # 实现了i的的更新,为下一趟做准备
    return records


def qk_pass(records: Records, low, high):
    """
    对记录数组records.record_list中的r[low]至r[high]部分进行一趟排序,并得到基准的位置,使得排序后的结
    果满足其之后(前)的记录的关键字均不小于(大于)基准记录
    :param records:
    :param low:
    :param high:
    :return:
    """
    x = records.record_list[low]  # 选择基准记录
    while low < high:
        while low < high and x.key <= records.record_list[high].key:  # high从右到左找小于x.key的记录
            high -= 1
        if low < high:
            # 找到小于x.key的记录,则送入“空单元”records.record_list[low]
            records.record_list[low] = records.record_list[high]
            low += 1
        while low < high and x.key >= records.record_list[low].key:  # low从左到右找大于x.key的记录
            low += 1
        if low < high:
            # 找到大于x.key的记录,则送入“空单元”records.record_list[high]
            records.record_list[high] = records.record_list[low]
            high -= 1
    records.record_list[low] = x  # 将基准记录保存到low==high的位置
    return low  # 返回基准记录的位置


def qk_sort_(records: Records, low, high):
    """
    对记录数组records.record_list[low...high]用快速排序算法进行排序

    :param records: 待排序的记录数组对象
    :param low:
    :param high:
    :return: None
    """
    if low < high:
        pos = qk_pass(records, low, high)  # 调用一趟快速排序,已枢轴元素为界划分两个子表
        qk_sort_(records, low, pos-1)  # 对左部子表快速排序
        qk_sort_(records, pos+1, high)  # 对右部子表快速排序


def qk_sort(records: Records):
    """
    快速排序

    :param records: 待排序的记录数组对象
    :return: 待排序的记录数组对象
    """
    low = 1
    high = records.record_len
    if low < high:
        qk_sort_(records, low, high)
    return records

# test_change_type_sort
from change_type_sort import Record, Records, bubble_sort, qk_sort


records = Records([Record(key) for key in [48, 62, 39, 77, 55, 14, 35, 98]])
records = bubble_sort(records)
print([record.key for record in records.record_list if record])
records = Records([Record(key) for key in [48, 62, 39, 77, 55, 14, 35, 98]])
records = qk_sort(records)
print([record.key for record in records.record_list if record])


"""
运行结果:
[14, 35, 39, 48, 55, 62, 77, 98]
[14, 35, 39, 48, 55, 62, 77, 98]

Process finished with exit code 0
"""

[引用《数据结构–用C语言描述 耿国华版》]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值