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, Nintegers 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<cstdio>
#include<algorithm>
using namespace std;
const int N = ;
int origin[N],tempOri[N],changed[N];
int n; bool showArray(int A[]){ //
for(int i = ; i <= n; i++){
printf("%d",A[i]); //
if(i < n) printf(" ");
}
printf("\n");
} bool isSame(int A[],int B[]){
for(int i = ; i <= n; i++){
if(A[i] != B[i]) return false;
}
return true;
} bool insertSort(){
bool flag = false;
for(int i = ; i <= n; i++){ //
if(i != && isSame(tempOri,changed)){ //
flag = true;
}
sort(tempOri,tempOri+i+); //
if(flag == true){
return true;
}
}
return false;
} void downAdjust(int low,int high){
int i = low, j = i*;
while(j <= high){
if(j < high && tempOri[j] < tempOri[j+]){
j = j+;
}
if(tempOri[i] < tempOri[j]){
swap(tempOri[i],tempOri[j]);
i = j;
j = j*;
}else{
break;
}
}
} void heap(){
bool flag = false;
for(int i = n/; i >=; i--){
downAdjust(i,n);
}
for(int i = n; i > ; i--){
if(i != n && isSame(tempOri,changed)){
flag = true;
}
swap(tempOri[i],tempOri[]);
downAdjust(,i-);
if(flag == true){
showArray(tempOri);
return; //
}
} } int main(){
scanf("%d",&n);
for(int i = ; i <= n; i++){
scanf("%d",&origin[i]);
tempOri[i] = origin[i];
}
for(int i = ; i <= n; i++){
scanf("%d",&changed[i]);
}
if(insertSort()){
printf("Insertion Sort\n");
showArray(tempOri);
}else{
printf("Heap Sort\n");
for(int i = ; i <= n; i++){
tempOri[i] = origin[i];
}
heap();
}
return ;
}

09-排序3 Insertion or Heap Sort (25 分)的更多相关文章

  1. PTA 09-排序3 Insertion or Heap Sort (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort   (25分) Accor ...

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

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

  3. 09-排序3 Insertion or Heap Sort (25 分)

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

  4. 1098 Insertion or Heap Sort (25分)

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

  5. pat1098. Insertion or Heap Sort (25)

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

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

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

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

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

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

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

  9. 1098. Insertion or Heap Sort (25)

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

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

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

随机推荐

  1. 如何边遍历集合边删除元素--使用Iterator中的remove()方法

    在遍历集合时,想将符合条件的某些元素删除,开始是用了下面的方法 public static void main(String[] args) throws UnsupportedEncodingExc ...

  2. java中的自动转型的学习理解

    java当中的继承是和c++中的继承类似,只是java中的继承时的父类只能有一位. 我们今天在这里讲的是关于java中的自动转型的理解:顾名思义,自动转型值得就是使用时自动的将自身的类型进行转化. 自 ...

  3. python爬虫实战(3)--图片下载器

    本篇目标 1.输入关键字能够根据关键字爬取百度图片 2.能够将图片保存到本地文件夹 1.URL的格式 进入百度图片搜索apple,这时显示的是瀑布流版本,我们选择传统翻页版本进行爬取.可以看到网址为: ...

  4. Eclipse中,将tab缩进改为4个空格

    用4个空格来缩进 , 不要用Tab来缩进 , 因为Tab在不同平台的点位不一样 eclipse->preferences->General->Editors->Text Edi ...

  5. WPA密码攻击宝典

    原则:密码以8-10位为主.11位仅限于当地手机号.一般人的多年用数字做密码的习惯和心理,先数 字.再字母,或数字.字母重复几遍,字符几乎全用小写,所以淘汰大写及"~!@#$%^&* ...

  6. ArcGIS10拓扑规则-面规则(转)

    ArcGIS10拓扑规则-面规则 原创 2013年12月27日 10:20:44 标签: ArcGIS 1879 ARCGIS 10 里提供的拓扑规则共32种,下面一一介绍: 首先介绍的对于面图层的拓 ...

  7. Linux 下安装redis

    记录一下linux下的安装步骤,还是比较复杂的 1. 下载redis-2.8.19.tar.gz: ftp传到linux01上: 解压: tar –zxvf redis-2.8.19.tar.gz 2 ...

  8. [转]AJAX工作原理及其优缺点

    1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术.它使用:使用XHTML ...

  9. oracle数据库输入conn / as sysdba 出现ORA-01031: insufficient privileges + 忘记sys密码如何改密码

    今天忘记了oracle数据库sys用户的密码,想着直接改密码输入conn / as sysdba 出现了ORA-01031: insufficient privileges(权限不足)的错误,到处搜教 ...

  10. 每个程序中只有一个public类,主类?

    import java.io.*; public class GameSaverTest { public static void main(String[] args){ //创建人物 GameCh ...