Insert or Merge && Insertion or Heap Sort
原题连接:https://pta.patest.cn/pta/test/1342/exam/4/question/27102
题目如下:
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.
Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.
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 NNN (≤100\le 100≤100). Then in the next line, NNN integers are given as the initial sequence. The last line contains the partially sorted sequence of the NNN 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 "Merge 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 0 6
1 3 2 8 5 7 4 9 0 6
Sample Output 2:
Merge Sort
1 2 3 8 4 5 7 9 0 6
______________________________________________________________________________________________________________________________________________________________________________
感觉这道题目的题意比较理解,两种排序方法,但给出的样例是排序到其中某一排序的中间一步,至于是哪一步,还得自己写程序判断。以下是代码,其中某些地方也借鉴了其他人的写法,但记不清是哪位同学的了……
#include<stdio.h>
#define Max 105
int IsSort(int *a,int *b,int N)
{
int i;
for (i=;i<N;i++)
{
if (a[i]!=b[i])return ;
}
return ;
} void InsertSort(int A[],int p)
{
int tmp=A[p];
int i;
for (i=p;i> && A[i-]>tmp;i--)A[i]=A[i-];
A[i]=tmp;
} void Merge(int A[],int tmpA[],int L,int R,int RightEnd)
{
int LeftEnd,Number,tmp,i;
LeftEnd=R-;
Number=RightEnd-L+;
tmp=L;
while (L<=LeftEnd && R<=RightEnd)
{
if (A[L]<=A[R])tmpA[tmp++]=A[L++];
else tmpA[tmp++]=A[R++];
}
while (L<=LeftEnd)tmpA[tmp++]=A[L++];
while (R<=RightEnd)tmpA[tmp++]=A[R++];
for (i=;i<Number;i++,RightEnd--)
{
A[RightEnd]=tmpA[RightEnd];
} } void MSort(int A[],int tmpA[],int length,int N)
{
int i,j;
for (i=;i<=N-*length;i+=*length)
{
Merge(A,tmpA,i,i+length,i+*length-);
}
if (i+length<N)Merge(A,tmpA,i,i+length,N-); } void pri(int *a,int N)
{
int i;
for (i=;i<N;i++)
{
if (i==)printf("%d",a[i]);
else printf(" %d",a[i]);
} }
int main()
{
int N,a1[Max],a2[Max],b[Max],tmpA[Max],i,v;
scanf("%d",&N);
for (i=;i<N;i++)
{
scanf("%d",&v);
a1[i]=a2[i]=v;
}
for (i=;i<N;i++)scanf("%d",&b[i]); for (i=;i<N;i++)
{
InsertSort(a1,i);
if (IsSort(a1,b,N))
{
printf("Insertion Sort\n");
InsertSort(a1,i+);
pri(a1,N);
return ;
}
} int length=;
while (length<N){
MSort(a2,tmpA,length,N);
length*=;
if (IsSort(a2,b,N))
{
printf("Merge Sort\n");
MSort(a2,tmpA,length,N);
pri(a2,N);
return ;
}
} }
________________________________________________________________________________________________________________________
接下来还有一道题和该题类似,只不过把归并排序换成了堆排序
#include<stdio.h>
#define Max 100
int IsSort(int *a,int *b,int N)
{
int i;
for (i=;i<N;i++)
{
if (a[i]!=b[i])return ;
}
return ;
} int InsertionSort(int *a,int p)
{
int i,tmp;
tmp=a[p];
for (i=p;i> && a[i-]>tmp ;i--)
{
a[i]=a[i-];
}
a[i]=tmp;
} void Swap(int *a,int *b)
{
int tmp;
tmp=*a;*a=*b;*b=tmp;
} void PercDown(int *a,int p,int N)
{
int tmp,Child,Parent;
tmp=a[p];
for (Parent=p;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 HeapSort(int *a,int N)
{
int i;
for (i=N/2-1;i>=0;i--)PercDown(a,i,N);
for (i=N-1;i>0;i--)
{Swap(&a[0],&a[i]);
PercDown(a,0,i);}
} */ void Pri(int *a,int N)
{
int i;
for (i=;i<N;i++)
{
if (i==)printf("%d",a[i]);
else printf(" %d",a[i]);
}
} int main()
{
int i,N,a1[Max],a2[Max],b[Max],v;
scanf("%d",&N);
for (i=;i<N;i++)
{
scanf("%d",&v);
a1[i]=a2[i]=v;
}
for (i=;i<N;i++)scanf("%d",&b[i]); for (i=;i<N;i++)
{
InsertionSort(a1,i);
if (IsSort(a1,b,N))
{
printf("Insertion Sort\n");
InsertionSort(a1,++i);
Pri(a1,N);
return ;
}
} for (i=N/-;i>=;i--)PercDown(a2,i,N); //Build the Heap; for (i=N-;i>;i--)
{
Swap(&a2[],&a2[i]);
PercDown(a2,,i);
if (IsSort(a2,b,N))
{
printf("Heap Sort\n");
Swap(&a2[],&a2[--i]);
PercDown(a2,,i);
Pri(a2,N);
return ;
}
else printf("df");
}
}
Insert or Merge && Insertion or Heap Sort的更多相关文章
- PAT甲级1098. Insertion or Heap Sort
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
- PTA Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1098 Insertion or Heap Sort
1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...
- PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...
- 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 ...
- 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 ...
- Insertion or Heap Sort
7-14 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one inp ...
- PAT_A1098#Insertion or Heap Sort
Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: According to Wikipedia: Insertion sort ...
随机推荐
- angular1.x的简单介绍 (一)
angular1.x作为经典的mvc框架,可以创建能够复用的组件,也可进行双向数据绑定.国内的vue.js/avaloon.js都是同类型的框架.适合使用angularjs的项目有大型信息化管理系统: ...
- 数据库邮件服务器中sp_send_dbmail的参数使用
sp_send_dbmail [ [ @profile_name = ] 'profile_name' ] [ , [ @recipients = ] 'recipients [ ; n ]' ...
- Android基础学习第三篇—Intent的用法
写在前面的话: 1. 最近在自学Android,也是边看书边写一些Demo,由于知识点越来越多,脑子越来越记不清楚,所以打算写成读书笔记,供以后查看,也算是把自己学到所理解的东西写出来,献丑,如有不对 ...
- CGI与fastcgi与php-fpm与php-cgi的关系
cgi是一个协议,它规定了服务器Nginx会将那些数据传送给PHP-cgi fastcgi也可以说是一个协议.fastcgi是对cgi的性能的一次提高.fastcgi会先启动一个master,解析配置 ...
- 安装spark ha集群
安装spark ha集群 1.默认安装好hadoop+zookeeper 2.安装scala 1.解压安装包 tar zxvf scala-2.11.7.tgz 2.配置环境变量 vim /etc/p ...
- Open 语法的使用
我们通常会需要在命令中,打开文件输入信息,在python中我们就会使用open语法,进行此方面的操作.详细方式如下:#Python open 函数# 作用:打开一个文件# 语法:open(file[, ...
- hibernate框架int和Integer类型区别
hibernate 框架在定义实体时,int类型最好定义为Inttger类型,因为在注入时int是值类型不允许为空.
- 通过sql server 连接mysql
图文:通过sql server 连接mysql 1.在SQL SERVER服务器上安装MYSQL ODBC驱动; 驱动下载地址:http://dev.mysql.com/downloads/con ...
- .NET平台常用的框架整理
基于.NET平台常用的框架整理 DotNet | 2016-03-31 17:13 (点击上方蓝字,可快速关注我们) 来源:天使不哭 链接:http://www.cnblogs.com/hgmyz/p ...
- PDA手持机 移动开单进销存系统 现场出打印凭据和扫码 新的亮点
传统车销模式弊端:1.手写开单,效率低,动作慢2.现场手写开单明细不能打印,产品明细不规范3.电脑办公人员及车销人员对车上的库存情况掌握不清楚,销售人员对每种产品销售价格不清楚4.老板对员工工作的管控 ...