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 (≤). 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
 //注意,这里的排序为排序迭代过程中完成的某一步而已,并不是最终结果
//我们可以直接用sort来代替排序的部分,无论是合并排序还是插入排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, a;
vector<int>nums, ans, temp;
void down(int index, int n)
{
int t = temp[index];
while ( * index + < n)
{
int child = * index + ;
if (child + < n && temp[child] < temp[child + ])
++child;
if (temp[child] > t)
{
temp[index] = temp[child];
index = child;
}
else
break;
}
temp[index] = t;
}
int main()
{
cin >> N;
for (int i = ; i < N; ++i)
{
cin >> a;
nums.push_back(a);
}
for (int i = ; i < N; ++i)
{
cin >> a;
ans.push_back(a);
}
temp = nums;
bool f = false;
for (int i = ; i < N; ++i)//进行插入排序
{
sort(temp.begin(), temp.begin() + i + );//第一趟排序应该是排序前两个数,第i趟排序分别排序前i+1个数
if (temp == ans)
{
f = true;
cout << "Insertion Sort" << endl;
sort(temp.begin(), temp.begin() + i + );//再一次迭代
break;
}
}
if (f == false)//那就是堆排序
{
cout << "Heap Sort" << endl;
temp = nums;
for (int i = N / ; i >= ; --i)//将前一半元素进行下滤
down(i, N);
for(int i=N-;i>;--i)//N-1趟排序,每次排序得到一个第N-i大的值放到相应位置
{
swap(temp[i], temp[]);
down(, i);
if (temp == ans)
{
swap(temp[i-], temp[]);//进行下一次迭代
down(, i - );
break;
}
}
}
for (int i = ; i < N; ++i)
cout << temp[i] << (i < N - ? " " : "");
return ;
}

PAT甲级——A1098 Insertion or Heap Sort的更多相关文章

  1. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

  2. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  3. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. PAT A1098 Insertion or Heap Sort (25 分)——堆排序和插入排序,未完待续。。

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  5. A1098. Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  6. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

    题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...

  7. 1098 Insertion or Heap Sort——PAT甲级真题

    1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  8. PTA Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. 1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...

随机推荐

  1. 用CSS做导航菜单的4个理由

    导航结构在网站设计中是起到决定性作用的,导航菜单/栏常常通过颜色.排版.形状和一些图片来帮助网站创造更好的视觉和感受,它是网页设计的关键元素.虽然网站导航菜单的外观是网页设计中关系到整个设计成败与否的 ...

  2. javascript面向对象编程笔记(函数之闭包)

    3 函数 3.5 闭包(closures) 3.5.1 作用域链 与很多程序设计语言不同,javascript不存在大括号级的作用域,但它有函数作用域,即在函数内定义的变量在函数外是不可见的.但如果该 ...

  3. 2019牛客暑期多校训练营(第八场)D-Distance 定期重构

    题目传送门 题意: 在一个三维空间中,给出q次操作,每次操作可以在空间中加上一个固定点,或者询问一个点,对于一个询问操作,输出距离这个点最近的固定点的曼哈顿距离. 思路: 官方题解:先假设所有询问都在 ...

  4. JavaScript 点击事件的三种写法

    嵌入式 <button οnclick='alert("hello")'>点击按钮</button> 脚本模型 btn.onclick=function() ...

  5. dp转图论——cf1070A好题

    dp的状态转移很像一张有向图:每个状态为一个点,每中转移方案是一条有向边 本题要求是求出最小的数,那我们用状态[i,j]表示模i,数位和为j,那么从每个点出发的十条有向边代表[0,9]十个数 从每个状 ...

  6. HttpClient 使用案例

    package com.qifeng.config.ygx.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fas ...

  7. python相关小技巧整理[持续更新]

    1. pdb的非常方便的debug,抛弃print吧~ 参考https://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/ impor ...

  8. Java 的锁-老王女儿的爱情

    对象锁: new一个对象,都会给这个实例创建一把锁,对象中的方法必须在实例创建后,通过调用方法获取锁,一个线程进去这个方法之前拿到对象的锁,才能调用方法,否则被阻塞,举个例子,老王有个如花似玉的女儿, ...

  9. 安装MySql社区版(35-3)

    1,https://dev.mysql.com/ --------------------------------------------------------------------------- ...

  10. mybatis-plus分页查询

    在springboot中整合mybatis-plus 按照官方文档进行的配置:快速开始|mybatis-plus 引入依赖: <!-- 引入mybatisPlus --> <depe ...