According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At 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 resuling 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>
using namespace std;
int main(){
int N; cin>>N;
int i,j,flag=1,tag=0;
int num0[N],num1[N];
for(i=0;i<N;i++)
cin>>num0[i];
for(i=0;i<N;i++)
cin>>num1[i];
for(i=0;i<N-1;i++)
if(num1[i]>num1[i+1]) break;
for(j=i+1;j<N;j++)
if(num1[j]!=num0[j]) flag=0;
if(flag==0){
cout<<"Heap Sort"<<endl;
for(j=1;j<N;j++)
if(num1[(j-1)/2]<num1[j]) break;
j=j-1;
int temp=num1[j];
num1[j]=num1[0];
num1[0]=temp;
j--;
int parent,child;
temp=num1[0];
for(parent=0;2*parent+1<=j;parent=child){
child=parent*2+1;
if(child<j&&num1[child+1]>num1[child])
child++;
if(num1[child]>temp)
num1[parent]=num1[child];
else
break;
}
num1[parent]=temp;
}
else{
cout<<"Insertion Sort"<<endl;
int temp=num1[i+1];
for(i=i+1;i>0;i--)
if(num1[i-1]>temp) num1[i]=num1[i-1];
else break;
num1[i]=temp;
}
for(int k=0;k<N;k++)
if(tag++==0) cout<<num1[k];
else cout<<" "<<num1[k];
return 0;
}

PAT 1098. 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. 1098 Insertion or Heap Sort——PAT甲级真题

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

  5. 1098 Insertion or Heap Sort

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

  6. PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)

    http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...

  7. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  8. PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

    题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...

  9. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

随机推荐

  1. 通过scp批量推送文件到远程目录

    [root@openfire1 script]# cat test.sh  #!/bin/bash   #通过scp推送文件到远程目录 for ip in `cat iplist`  do       ...

  2. Excel 常用快捷键键 快捷方式

    移动整行的位置 Shift + Alt + 鼠标拖拽 不加Shit + Alt 移动后 留白 注意:需要移动鼠标到行的最上面,变成十字箭头

  3. luogu 3415 祭坛

    题目大意: 在平面上,有 n 个水晶柱,每个水晶柱可以用一个点表示 如果 4 个水晶柱依次相连可以构成一个四边形,满足其两条对角线分别平行于 x 轴和 y 轴,并且对角线的交点位于四边形内部(不包括边 ...

  4. [模板]FWT

    写起来和fft很像,这里放个板子. 代码: #include<iostream> #include<cstdio> #include<cmath> #include ...

  5. Luogu1275魔板

    https://zybuluo.com/ysner/note/1136271 题面 有这样一种魔板:它是一个长方形的面板,被划分成\(n\)行\(m\)列的\(n*m\)个方格.每个方格内有一个小灯泡 ...

  6. E20171005-ts

    collapse  n. 垮台; (身体的) 衰弱;              vt. 使倒塌; 使坍塌; 使瓦解;               vi. 崩溃; 倒塌; 折叠; (尤指工作劳累后) 坐 ...

  7. [Swift通天遁地]六、智能布局-(7)通过Group(组)命令实现对多个视图的统一约束

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. Akka源码分析-Extension

    一个设计优秀的工具或框架,应该都有一个易用.强大的插件或扩展体系,akka也不例外. akka的扩展方法非常简单,因为只涉及到两个组件:Extension. ExtensionId.其中Extensi ...

  9. openpyxl python操作Excel表格,

    这里openpyxl只支持xlsx格式的Excel,openpyxl使用起来会更方便一些,所以如果只操作小流水线文件的话,那么可以优先选择openpyxl,如果要兼容xls的话,就使用xlrd/xlw ...

  10. 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

    Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and ...