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 ...
随机推荐
- Sass 的调试
Sass 的调试 Sass 调试一直以来都是一件头痛的事情,使用 Sass 的同学都希望能在浏览器中直接调试 Sass 文件,能找到对应的行数.值得庆幸的是,现在实现并不是一件难事,只要你的浏览器支持 ...
- cnentos中进行bond网卡配置,一切配置无问题,就是ping不通宿主机
服务器网口绑定 1. ifcfg-bond0 DEVICE=bond0 ONBOOT=yes IPADDR=192.168.100.64 NETMASK=255.255.255.0 2. ...
- c# 保存数据到txt (追加)
StringBuilder sb = new StringBuilder(); sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:s ...
- 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
第一步: windows 8系统,运行aspx页面出现上述错误信息,解决办法如下: iis7 :控制面板->打开或关闭windows功能->Internet信息服务->万维网服务-& ...
- 如何生成git的公钥和私钥
转载地址:http://blog.csdn.net/wqjsir/article/details/17386087 一. Git windows 客服端(MsysGit)下载 下载地址:http:// ...
- Eclipse Java注释模板设置详解
设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...
- 当攻击者熟读兵法,Camouflage病毒实战演示暗度陈仓之计
"明修栈道,暗度陈仓"的典故许多人都听说过,该典故出自楚汉争霸时期,刘邦意图进入关中,需要攻下关中咽喉之地--陈仓.韩信献出一计:表面上浩浩荡荡地修复通往陈仓的栈道以迷惑陈仓守将, ...
- 【DWR系列01】-DWR简介及入门例子
.literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...
- POJ 2991–Crane【线段树+几何】
题意: 把手臂都各自看成一个向量,则机械手的位置正好是手臂向量之和.旋转某个关节,其实就是把关节到机械手之间的手臂向量统统旋转. 由于手臂很多,要每个向量做相同的旋转操作很费时间.这时就可以想到用线段 ...
- Windows7 64位系统搭建Cocos2d-x-2.2.1最新版以及Android交叉编译环境(详细教程)
Windows7 64位系统搭建Cocos2d-x-2.2.1最新版以及Android交叉编译环境(详细教程) 声明:本教程在参考了以下博文,并经过自己的摸索后实际操作得出,本教程系本人原创,由于升级 ...