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
 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int A[];
int B[];
int L;
void Swap(int *C,int i, int j)
{
int temp = C[i];
C[i] = C[j];
C[j] = temp;
}
void Print(int N)
{
int i;
for (i = ; i < N - ; i++)
printf("%d ", B[i]);
printf("%d", B[i]);
} int Charge(int N)
{
int i;
if (B[] > B[] && B[] > B[])
return ;
else
return ;
} void Insert_Once(int i, int N)
{
int j;
int Temp = B[i];
for (j = i; j > && B[j - ] > Temp; j--)
B[j] = B[j - ];
B[j] = Temp;
} void PrecDown(int i,int N)
{
int Parent, Child;
int Tmp = A[i];
for (Parent = i; Parent * + <= N; Parent = Child)
{
Child = Parent * + ;
if (Child != N && A[Child] < A[Child + ])
Child++;
if (Tmp >= A[Child])break;
else
A[Parent] = A[Child];
}
A[Parent] = Tmp;
}
void BuildMaxHeap(int N)
{
for (int i = (N - ) / ; i >= ; i--)
PrecDown(i, N - );
}
void Heap_Sort(int N)
{
for (int i = N - ; i >= ; i--)
{
Swap(A, , i);
PrecDown(, i-);
}
}
int FindI(int N)
{
int i;
for (i = N - ; i >= && A[i] == B[i]; i--);
return i;
}
void PrecDown_Once(int i)
{
Swap(B,, i);
int Parent, Child;
int tmp;
tmp = B[];
for (Parent = ; (Parent * ) + <= i-; Parent = Child)
{
Child = (Parent * ) + ;
if (Child != i-&& B[Child]<B[Child + ])
Child++;
if (tmp >= B[Child])break;
else
B[Parent] = B[Child];
}
B[Parent] = tmp;
}
void Heap_Once(int N)
{
BuildMaxHeap(N);
Heap_Sort(N);
int i = FindI(N);
PrecDown_Once(i);
}
int main()
{
int N;
int Flag = ;
int OrderPosition = ;
scanf("%d", &N);
for (int i = ; i < N; i++)
scanf("%d", &A[i]);
for (int i = ; i < N; i++)
scanf("%d", &B[i]);
if (Flag = Charge(N))
printf("Heap Sort\n");
else
printf("Insertion Sort\n");
for (int i = ; i < N - ; i++)
if (B[i] <= B[i + ]) //这里必须是大于等于
OrderPosition = i + ;
else
break;
if (Flag)
Heap_Once(N);
else
Insert_Once(OrderPosition + , N);
Print(N);
return ;
}

最新文章

  1. 解决VS调试时断点不会命中
  2. UITableView代理方知多少+执行顺序
  3. Innodb引擎 compact模式下元组的磁盘存储结构
  4. 问题解决——multimap中统计key的种类
  5. HDU 1054 Strategic Game(最小点覆盖+树形dp)
  6. 利用NTP搭建自己的ntp服务
  7. MySQL安装配置,命令,异常纪要
  8. Ninject 自动注册
  9. javascript中的稀疏数组(sparse array)和密集数组
  10. STL容器的内存分配
  11. 第2章 Python基础语法 -- 数据类型
  12. 初次使用IntelliJ IDEA 2016.2
  13. ural2062 Ambitious Experiment
  14. tmux简要介绍
  15. .net 4.0 中的特性总结(二):默认参数、命名参数
  16. unset与unlink
  17. 在Linux/Centos下用wondershaper限速
  18. tensorflow 1.0 学习:十图详解tensorflow数据读取机制
  19. WMS专业术语&amp;系统功能操作培训
  20. MVC Log4Net 配置

热门文章

  1. 浅谈JS之setTimeout与setInterval
  2. 关于.NET中的控制反转及AutoFac的简单说明
  3. 03 Uipath调用VBA脚本,处理excel文档格式
  4. 单选框 改成 复选框 的css样式
  5. Spring扩展:替换IOC容器中的Bean组件 -- @Replace注解
  6. Journal of Proteome Research | Prediction of an Upper Limit for the Fraction of Interprotein Cross-Links in Large-Scale In Vivo Cross-Linking Studies (分享人:张宇星)
  7. jupyter之配置自己喜欢的python环境
  8. GitHub 热点速览 Vol.13:近 40k star 计算机论文项目再霸 GitHub Trending 榜
  9. 8千字干货教程|java反射精讲
  10. bluekeep漏洞(CVE-2019-0708)利用