当前位置: 首页 > news >正文

09-排序3 Insertion or Heap Sort(浙大数据结构)

09-排序3 Insertion or Heap Sort

分数 25

作者 陈越

单位 浙江大学

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resulting sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9

#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 110;
int heap[maxn],a[maxn],res[maxn];
int n;

bool InsertSort(int *A,int num)
{
    int flag=0;
    for(int i=2;i<=num;++i)
    {
        int temp=A[i],j;
        for(j=i;j>1 && A[j-1]>temp;--j)
            A[j]=A[j-1];
        A[j]=temp;

        if(flag==1)
            return true;

        for(int k=1;k<=num;++k)
        {
            if(A[k]==res[k])
                flag=1;
            else
            {
                flag=0;
                break;
            }
        }
    }

    return false;
}

void downAdjust(int low,int high)
{
    int par=low,chi=2*par;
    while(chi<=high)
    {
        if(chi!=high && heap[chi+1]>heap[chi])
            ++chi;
        if(heap[chi]>heap[par])
        {
            swap(heap[chi],heap[par]);
            par=chi;
            chi*=2;
        }
        else
            break;
    }
}

void CreateHeap(int num)
{
    for(int i=num/2;i>0;--i)
        downAdjust(i,num);
}

void HeapSort(int num)
{
    CreateHeap(num);

    int flag=0;
    for(int i=num;i>0;--i)
    {
        swap(heap[1],heap[i]);
        downAdjust(1,i-1);

        if(flag==1)
            return;
        for(int j=1;j<=num;++j)
        {
            if(heap[j]==res[j])
                flag=1;
            else
            {
                flag=0;
                break;
            }
        }
    }
}

int main()
{
    cin >> n;
    for(int i=1;i<=n;++i)
    {
        cin >> heap[i];
        a[i]=heap[i];
    }

    for(int i=1;i<=n;++i)
        cin >> res[i];

    if(InsertSort(a,n)==true)
    {
        puts("Insertion Sort");
        for(int i=1;i<=n;++i)
        {
            if(i==1)
                cout << a[i];
            else
                cout << ' ' << a[i];
        }
        cout << endl;
    }
    else
    {
        HeapSort(n);

        puts("Heap Sort");
        for(int i=1;i<=n;++i)
        {
            if(i==1)
                cout << heap[i];
            else
                cout << ' ' << heap[i];
        }
        cout << endl;
    }
    return 0;
}

相关文章:

  • java-python+vue社区防疫服务管理系统网站
  • sparksql insertinto 源码解析
  • 反射之获取Class
  • 【博客477】prometheus-----数值数据编码(varint与zigzag)
  • LCMXO2-2000HC-4FTG256C FPGA MachXO2系列 256-FTBGA 现场可编程门阵列
  • 初始Cpp之 四、数据类型
  • office2019如何自定义安装位置?
  • java基于ssm的汽车维修保养管理系统
  • Std::optional 源码分析
  • 目标检测——关键点检测学习记录(四):人脸和手部特征点检测
  • IMX6ULL学习笔记(5)——获取和编译U-Boot
  • 尚硅谷Vue系列教程学习笔记(14)
  • linux虚拟机mysql
  • golang基于errgroup实现并发调用
  • 【python中级】linux系统获得计算机网卡流量
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • 【RocksDB】TransactionDB源码分析
  • Android优雅地处理按钮重复点击
  • Angular数据绑定机制
  • eclipse的离线汉化
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • ES2017异步函数现已正式可用
  • HTTP 简介
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • Redux系列x:源码分析
  • vue数据传递--我有特殊的实现技巧
  • Webpack 4 学习01(基础配置)
  • 闭包--闭包作用之保存(一)
  • 给Prometheus造假数据的方法
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 蓝海存储开关机注意事项总结
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 区块链分支循环
  • 如何进阶一名有竞争力的程序员?
  • 树莓派 - 使用须知
  • 突破自己的技术思维
  • 线上 python http server profile 实践
  • 小程序 setData 学问多
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #android不同版本废弃api,新api。
  • #AngularJS#$sce.trustAsResourceUrl
  • #pragma once与条件编译
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (DFS + 剪枝)【洛谷P1731】 [NOI1999] 生日蛋糕
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (Note)C++中的继承方式
  • (附源码)php新闻发布平台 毕业设计 141646
  • (十三)Flask之特殊装饰器详解
  • (数据结构)顺序表的定义