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 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h> void PrintA(int A[], int N)
{
int i;
for(i=;i<N;i++) {
if(i != N-)
printf("%d ", A[i]);
else
printf("%d\n", A[i]);
}
} int IsSameArr(int A[], int C[], int N)
{
int i;
for(i=;i<N;i++)
if(A[i] != C[i]) return ;
return ;
} void CopyArr(int A[], int B[], int N)
{
int i;
for(i=;i<N;i++)
B[i] = A[i];
} void Read(int A[], int B[], int N)
{
int i;
for(i=;i<N;i++) {
if(i!=N-)
scanf("%d ", &A[i]);
else
scanf("%d\n", &A[i]);
} for(i=;i<N;i++) {
if(i!=N-)
scanf("%d ", &B[i]);
else
scanf("%d\n", &B[i]);
}
} int InsertionSort(int A[], int B[], int N)
{
int P, i;
int Tmp, ret = ;
int flag = ; for(P=;P<N;P++) {
Tmp = A[P];
for(i=P;i> && A[i-]>Tmp;i--)
A[i] = A[i-];
A[i] = Tmp;
if(IsSameArr(A, B, N)) {
printf("Insertion Sort\n");
flag = ;
ret = ;
continue;
}
if(flag== ) {
flag = ;
PrintA(A, N);
}
}
if(flag == )
PrintA(A, N); return ret;
} void PrecDown(int A[], int P, int N)
{
int Parent, Child, temp = A[P];
for(Parent=P;Parent*+<N;Parent=Child) {
Child = Parent*+;
// printf("Child=%d A[Child]=%d temp=%d \n", Child, A[Child], temp);
if((Child < N-)&&(A[Child+] > A[Child]))
Child++;
if(temp < A[Child])
A[Parent] = A[Child];
else
break; // printf("[PecDown] ");
// PrintA(A, N);
}
A[Parent] = temp;
} void Swap(int *a, int *b)
{
int tmp;
tmp = *b;
*b = *a;
*a = tmp;
} void Heap_Sort(int A[], int B[], int N)
{
int i, flag = ;
// PrintA(A, N);
for(i=N/-;i>=;i--) {
// printf("Heap_Sort[i] = %d\n", i);
PrecDown(A, i, N);
if(IsSameArr(A, B, N)) {
printf("Heap Sort\n");
flag = ;
continue;
}
if(flag == ) {
flag = ;
PrintA(A, N);
}
} // printf("-----------\n"); for(i=N-;i>;i--) {
Swap(&A[], &A[i]);
PrecDown(A, , i);
if(IsSameArr(A, B, N)) {
printf("Heap Sort\n");
flag = ;
continue;
}
if(flag == ) {
flag = ;
PrintA(A, N);
}
}
} int main()
{
int N;
int *A, *B, *Tmp;
scanf("%d\n", &N);
A = (int *)malloc(sizeof(int)*N);
B = (int *)malloc(sizeof(int)*N);
Tmp = (int *)malloc(sizeof(int)*N);
Read(A, B, N);
CopyArr(A, Tmp, N);
if(!InsertionSort(Tmp, B, N)) {
CopyArr(A, Tmp, N);
Heap_Sort(Tmp, B, N);
}
return ;
}
 

最新文章

  1. grunt安装和使用教程
  2. 使用joi来验证数据模型
  3. HTML5+学习笔记1-------边看代码边研究中
  4. LeetCode题解——3Sum Closest
  5. String对象不可改变的特性
  6. Decimal:解决0.3 != 0.1+0.1+0.1的方法(十进制浮点数)
  7. HTML系列(五):超链接
  8. Context Switch and System Call
  9. cocos2d-x 获得系统语言繁体
  10. Azure Messaging-ServiceBus Messaging消息队列技术系列7-消息事务
  11. Python自学笔记-面向对象编程(Mr seven)
  12. Android高效率编码-第三方SDK详解系列(一)——百度地图,绘制,覆盖物,导航,定位,细腻分解!
  13. JDK源码分析(8)之 Reference 完全解读
  14. android开发_文本按钮 与 输入框
  15. 人工智能第三课:数据科学中的Python
  16. MySQL索引调优【转】
  17. FORM表单中onclick()、submit()与onsubmit()的问题
  18. 【JVM底层策略 一】GC roots如何判断对象不可达
  19. Miller_Rabin整理笔记
  20. 控制台中寄宿WCF服务

热门文章

  1. HBase备份还原OpenTSDB数据之Snapshot
  2. HTML--JS 9*9乘法口诀
  3. Mac011--DbWrench Database安装
  4. Java thread(4)
  5. 学习Spring IOC控制反转和DI依赖注入总结
  6. mybatis插件机制及分页插件原理
  7. Python 根据入栈顺利判定出栈顺序
  8. vue基础介绍及使用
  9. hdu5943 Kingdom of Obsession 二分图+打表找规律
  10. activity 生命周期 http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for