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. day35—JavaScript操作元素(创建、删除)

    转行学开发,代码100天——2018-04-20 JavaScript对DOM元素的创建.删除操作. 1.创建DOM元素 appendChild方法 createElement(ochild); op ...

  2. 解读:nginx的一个神秘配置worker_cpu_affinity

    今天在查看nginx的相关知识的时候发现了一个nginx之前不认识的配置:worker_cpu_affinity. nginx默认是没有开启利用多核cpu的配置的.需要通过增加worker_cpu_a ...

  3. Mac环境搭建nginx服务器

    一 安装 1 安装Homebrew macOS 缺失的软件包的管理器,安装方法参考官网.(可能被墙) 2 安装nginx. brew install nginx 先更新homebrew.再安装ngin ...

  4. AdaGrad Algorithm and RMSProp

    AdaGrad全称是Adaptive Gradient Algorithm,是标准Gradient Descent的又一个派生算法.标准Gradient Descent的更新公式为: 其中Learni ...

  5. libOpenThreads库链接冲突错误

    最近在linux 上安装了3.7.0版本的OpenSceneGraph,而在安装之前没有完全卸载之前安装的3.6.3版本,导致在编译程序链接时出现库引用冲突,在便以后出现以下警告信息: /usr/bi ...

  6. python的正则

    一.认识模块  什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.py文件 ...

  7. Pandas matplotlib 无法显示中文

    Pandas 无法显示中文问题 解决方案 Pandas在绘图时,会显示中文为方块,主要原因有二:   matplotlib 字体问题seaborn 字体问题 (实际上,matplotlib是支持uni ...

  8. Web Service-第一篇什么是Web Service

    1.什么是WebService? WebService是一种跨编程语言和跨操作系统平台的远程调用技术.WebService就是一个应用程序向外界暴露出一个能通过Web进行调用的API,也就是说能用编程 ...

  9. stl(set和pair)

    D - 4 Gym - 100989D In this cafeteria, the N tables are all ordered in one line, where table number ...

  10. python学习三十三天函数匿名函数lambda用法

    python函数匿名函数lambda用法,是在多行语句转换一行语句,有点像三元运算符,只可以表示一些简单运算的,lambda做一些复杂的运算不太可能.分别对比普通函数和匿名函数的区别 1,普通的函数用 ...