博客
关于我
程序设计入门21 堆排序与插入排序
阅读量:391 次
发布时间:2019-03-05

本文共 2228 字,大约阅读时间需要 7 分钟。

为了判断给定的排序结果是插入排序还是堆排序,并模拟下一次迭代,我们可以按照以下步骤进行:

判断排序方法

  • 插入排序的特征:每次插入一个元素到已排序位置,结果序列的已排序部分是递增的,且未排序部分的元素尚未被处理。
  • 堆排序的特征:每次移动未排序部分的最大元素到已排序位置,结果序列的已排序部分是递增的,且未排序部分可能包含较小的元素。
  • 通过观察结果序列的结构,判断已排序部分是否是递增的,并比较未排序部分是否有最大值已经被移动到已排序位置。如果满足堆排序的特征,则为堆排序,否则为插入排序。

    模拟下一次迭代

    根据判断结果,模拟下一次迭代:

  • 插入排序:

    • 取出未排序部分的下一个元素。
    • 插入到已排序位置,找到正确的位置。
  • 堆排序:

    • 构建一个堆结构,每次取出堆顶(最大值),插入到已排序位置。
  • 代码实现

    def is_sorted(arr):    for i in range(len(arr)-1):        if arr[i] > arr[i+1]:            return False    return Truedef insertion_sort(arr):    sorted_part = []    unsorted_part = arr.copy()    while unsorted_part:        element = unsorted_part.pop(0)        for i in range(len(sorted_part)+1):            if element <= sorted_part[i]:                sorted_part.insert(i, element)                break    return sorted_partdef heap_sort(arr):    n = len(arr)    heap = [0] * n    for i in range(n):        heap[i] = arr[i]    # Build heap    for i in range(n, 1, -1):        parent = i // 2        if heap[parent] < heap[i]:            heap[parent], heap[i] = heap[i], heap[parent]    # Sort the heap    sorted_part = []    while heap:        max_val = heap[0]        sorted_part.append(max_val)        heap[0] = 0        i = 1        while i < len(heap) and heap[i] > 0:            if heap[i//2] < heap[i]:                heap[i//2], heap[i] = heap[i], heap[i//2]            i += 1    return sorted_partdef main():    import sys    input = sys.stdin.read().split()    ptr = 0    N = int(input[ptr])    ptr += 1    initial = list(map(int, input[ptr:ptr+N]))    ptr += N    target = list(map(int, input[ptr:ptr+N]))    ptr += N        if is_sorted(target):        print("Heap Sort")        sorted_part = heap_sort(initial)        next_target = sorted_part + initial[N - len(sorted_part) : ]        print(' '.join(map(str, next_target)))    else:        print("Insertion Sort")        sorted_part = insertion_sort(initial)        next_target = sorted_part + initial[N - len(sorted_part) : ]        print(' '.join(map(str, next_target)))    if __name__ == "__main__":    main()

    解释

  • is_sorted函数:检查数组是否已经排序。
  • insertion_sort函数:模拟插入排序,逐个插入元素到已排序位置。
  • heap_sort函数:模拟堆排序,构建堆并每次移动最大值到已排序位置。
  • main函数:读取输入,判断排序方法,并模拟下一次迭代,输出结果。
  • 该代码首先读取输入,判断排序方法,然后模拟下一次迭代,输出结果。通过判断已排序部分是否是递增的来确定排序方法,并根据方法模拟下一步。

    转载地址:http://bolwz.baihongyu.com/

    你可能感兴趣的文章
    python基础---->常用模块
    查看>>
    Python基础(2):控制结构
    查看>>
    Python基础 | 关于“循环”那些事
    查看>>
    python系列【仅供参考】:python之subprocess模块rsync拉取文件
    查看>>
    python系列【仅供参考】:python 远程rdp
    查看>>
    python基础
    查看>>
    Python基本语法与变量类型
    查看>>
    python基本数据类型(容器)- tuple list dict set
    查看>>
    python基本工资的调整方案_Python薪资又涨了!这可咋办!
    查看>>
    Python基准测试和性能分析内存管理和垃圾回收
    查看>>
    Python基于RESTful风格的接口自动化测试实战
    查看>>
    python基于flask搭建http服务(四)—— Docker容器化部署
    查看>>
    python基于flask搭建http服务(二)—— 实现Excel上传、数据清洗、入库
    查看>>
    python基于flask搭建http服务(三)—— 使用gunicorn部署项目
    查看>>
    python基于flask搭建http服务(一)—— 实现数据查询和Excel导出
    查看>>
    Python块键盘/鼠标输入
    查看>>
    python在心理学研究中的应用有哪些_心理学在线研究可用平台简介和应用进展
    查看>>
    python在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
    查看>>
    python在gpu上运行_【python】python开启GPU加速
    查看>>
    Python在def函数中使用for循环
    查看>>