09-排序3 Insertion or Heap Sort(25 分)
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 分)的更多相关文章
- 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 ...
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 09-排序3 Insertion or Heap Sort (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1098 Insertion or Heap Sort (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- pat1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
随机推荐
- Linux(RHEL7)下安装vsftp服务
1.安装vsftp(没有配置yum源的先配置yum源) yum install -y vsftpd 2.启动ftp服务 systemctl start vsftpd.service 3.打开防火墙 f ...
- 学习HTML5 全局属性 accesskey-title
属性classcontextmenu指定一个元素的上下文菜单.当用户右击该元素,出现上下文菜单dirdropzone指定是否将数据复制,移动,或链接,或删除idspellcheck检测元素是否拼写错误 ...
- 基于K-means Clustering聚类算法对电商商户进行级别划分(含Octave仿真)
在从事电商做频道运营时,每到关键时间节点,大促前,季度末等等,我们要做的一件事情就是品牌池打分,更新所有店铺的等级.例如,所以的商户分入SKA,KA,普通店铺,新店铺这4个级别,对于不同级别的商户,会 ...
- JSP基础--EL表达式
EL(表达式语言) 1 EL概述 1.1 EL的作用 JSP2.0要把html和css分离.要把html和javascript分离.要把Java脚本替换成标签.标签的好处是非Java人员都可以使用. ...
- thphp(tp5)项目网站从Apache换成nginx报500
thphp(tp5)项目网站从Apache换成nginx报500 百度了一下,查看资料是Nginx配置fastcgi.conf的问题,打开文件编辑既可,如下图:
- Hibernate入门2
实体类的编写规则 要求实体类的属性是私有的 要求实体类中的私有属性有公开的get和set方法(设置器和访问器) 要求实体类有一个属性作为唯一值(一般使用id值) 实体类属性建议不使用基本数据类型,使用 ...
- python-生成式的基本使用
生成式是python中的一种高级玩法,起码看起来显得要高级一点.它可以使用简单的一行代码实现列表.字典等数据类型的创建或数据类型的转换等任务.另外,它和生成器还有些许关联. 列表生成式 列表生成式即生 ...
- (经典文章uplink)Information capacity and power control in single-cell multiuser communications(1995)
摘要:本文在用户衰落被完美测量的情况下,提出一种可最大程度提高单小区多用户通信平坦衰落的信息容量的功率控制.主要特征为:在任何特定的时刻,只有一个用户在整个带宽上进行传输,并且在信道良好时为用户分配更 ...
- Netty之大动脉Pipeline
Pipeline 设计原理 Channel 与ChannelPipeline: 相信大家都已经知道,在Netty 中每个Channel 都有且仅有一个ChannelPipeline 与之对应,它们的组 ...
- [Bzoj1008][HNOI2008]越狱(组合计数)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 组合计数的简单题,可能越狱的方案数等于总方案数-不可能越狱的方案数,则: 总方案数 ...