原题连接: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. XML 基础

    linux下xml编辑器 vim gedit editix wonderful;免费30天;可以进行有效性检查 xerces oxygen 收费 xmlcopyedit serna free 是ser ...

  2. 使用JSOM检查文件或文件夹是否存在

    How to Check with SharePoint JSOM if File or Folder Exists Here's a code snippet showing how to use ...

  3. expect

    #!/usr/bin/expect -fset ipaddr "192.168.5.4"set passwd "123qwe"set timeout 30 sp ...

  4. js三级地区联动

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head ...

  5. mysql 分页查询

    mysql,; : mysql,; -last. //如果只给定一个参数,它表示返回最大的记录行数目: mysql; 个记录行 ,n. 动态传参的分页查询 SELECT * FROM table LI ...

  6. Bill的挑战(bzoj 1879)

    Description Input 本题包含多组数据. 第一行:一个整数T,表示数据的个数. 对于每组数据: 第一行:两个整数,N和K(含义如题目表述). 接下来N行:每行一个字符串. Output ...

  7. wamp2.5 配置多端口虚拟主机

    1.保证httpd.conf下 Include conf/extra/httpd-vhosts.conf LoadModule php5_module "D:/E/php/wamp/bin/ ...

  8. JavaScript call

    <script> function dog() { this.sound = "wangwang~"; this.shout = function () { alert ...

  9. 1250 Super Fast Fourier Transform(湘潭邀请赛 暴力 思维)

    湘潭邀请赛的一题,名字叫"超级FFT"最终暴力就行,还是思维不够灵活,要吸取教训. 由于每组数据总量只有1e5这个级别,和不超过1e6,故先预处理再暴力即可. #include&l ...

  10. vue简单使用

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...