原题连接: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的更多相关文章

  1. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

  2. PTA Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  3. 1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...

  4. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  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. 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 ...

  8. Insertion or Heap Sort

    7-14 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one inp ...

  9. PAT_A1098#Insertion or Heap Sort

    Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: According to Wikipedia: Insertion sort  ...

随机推荐

  1. 为WAMP中的mysql设置密码(默认为空)

    为WAMP中的mysql设置密码 WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 1.首先,通过WAMP打开mysql控制台. 提示 ...

  2. 高德地图纯js和html

    <!doctype html> <html> <head> <meta content="" charset="utf-8&qu ...

  3. acm 1002 算法设计

    最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...

  4. iOS 微信支付总结

    1.支付流程 https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_3 商户系统和微信支付系统主要交互说明: 步骤1:用户在商户APP中选择 ...

  5. 使用VBScript实现设置系统环境变量的小程序

    本人有点桌面洁癖,桌面上只放很少的东西,很多软件都用快捷键调出.最近频繁用到一个软件,我又不想放个快捷方式在桌面,也不想附到开始菜单,于是乎想将其所在目录附加到系统环境变量Path上,以后直接在运行中 ...

  6. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别

    WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...

  7. python3 与 pip3 安装与使用

    1. yum -y install openssl* (pip依赖ssl环境) 2.编译安装python3 下载地址:https://www.python.org/ftp/python/ .tgz c ...

  8. 使用PowerShell来修改文件访问,创建,修改时间属性

    Function Set-FileTimeStamps { Param ( [Parameter(mandatory=$true)] [string[]]$path, [datetime]$date ...

  9. Js实现string.format

    经常需要动态拼接html字符串,想到用类似于.net的string.format函数比较好,于是找了下,stackoverflow的代码: if (!String.prototype.format) ...

  10. Jquery判断变量是否为空

    var aaa=''; if(aaa) { //aaa不为空也不是不可识别对象时执行 } else { //aaa为空或不可识别时执行 } aaa必须是变量,对象的属性好像是不行,