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 ;
}
 

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. Java-访问控制权限

    Java面向对象-访问控制权限 Java中,可以通过一些Java关键字,来设置访问控制权限: 主要有 private(私有), package(包访问权限),protected(子类访问权限),pub ...

  2. scrapy基础笔记

    公众号原文 公众号排版更友好,建议查看公众号原文 前言 reference: https://www.tutorialspoint.com/scrapy/scrapy_quick_guide.htm ...

  3. 备份mysql的shell

    • #!/bin/bash• file_name=`date +%Y%m%d%H%M%S`• db_path='/usr/local/db_back/'• mysqldump -ulepus -p12 ...

  4. Unity 指定参数

    构造函数参数初始化 InjectionConstructor IContainer.RegisterType<T, Class>(new InjectionConstructor(&quo ...

  5. LeetCode 144. Binary Tree Preorder Traversal 动态演示

    先序遍历的非递归办法,还是要用到一个stack class Solution { public: vector<int> preorderTraversal(TreeNode* root) ...

  6. 基于Netty重构RPC框架

    下面的这张图,大概很多小伙伴都见到过,这是Dubbo 官网中的一张图描述了项目架构的演进过程.随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在 ...

  7. CentOS tcpdump的使用实例

    tcpdump是一个用于截取网络分组,并输出分组内容的工具.tcpdump凭借强大的功能和灵活的截取策略,使其成为类UNIX系统下用于网络分析和问题排查的首选工具. 选项: -A 以ASCII格式打印 ...

  8. Docker配置远程访问

    近来学习Docker部署微服务,需要配置Docker的远程访问,由于实际环境和学习资料有出入,尝试着根据网上搜索的一些相关资料进行配置,未能成功.最终通过自己摸索,成功配置Docker远程访问.现和大 ...

  9. Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法. 重启服务

    今天用Oracle突然出现Oracle ORA-01033: ORACLE initialization or shutdown in progress. 想起前两天删掉了几个DBF文件,幸好还没有清 ...

  10. Metasploitable2使用指南

    Metasploitable2使用指南 Metasploitable2 虚拟系统是一个特别制作的ubuntu操作系统,本身设计作为安全工具测试和演示常见漏洞攻击.版本2已经可以下载,并且比上一个版本包 ...