Insert or Merge && Insertion or Heap Sort

时间:2022-09-26 15:13:53

原题连接: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&period; 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 &lpar;插入排序、堆排序&rpar;

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

  5. pat1098&period; Insertion or Heap Sort &lpar;25&rpar;

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  6. pat 甲级 1098&period; Insertion or Heap Sort &lpar;25&rpar;

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  7. PTA 09-排序3 Insertion or Heap Sort &lpar;25分&rpar;

    题目地址 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&lowbar;A1098&num;Insertion or Heap Sort

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

随机推荐

  1. jquery取&lt&semi;input&gt&semi;的readOnly属性,O要大写

    今天在jquery中取input的readonly属性时,发现 我这样写$(“#input”).readonly取这个属性时,总是undefined,后来一想,难道html中的属性only没有大写,是 ...

  2. java comet

    http://www.javaworld.com/article/2077995/java-concurrency/asynchronous-processing-support-in-servlet ...

  3. 规划在sharepoint中使用安全组

    简介 如果将权限级别分配给组而非单个用户,那么管理 SharePoint 网站用户其实很简单.SharePoint 组是一组单独的用户,它还可以包含 Active Directory 组.在 Acti ...

  4. JavaScript 【 IE中的XML DOM 】

    IE中的 XML DOM 在统一的正式规范出来以前,浏览器对于XML的解决方案各不相同.DOM2级提出了动态创建XML DOM规范,DOM3进一步增强了XML DOM.所以,在不同的浏览器实现XML的 ...

  5. UIDocumentInteractionController 文件预览

    //创建并从底部弹出来 - (void)viewDidLoad { [super viewDidLoad]; [self setupDocumentControllerWithURL:fileURL] ...

  6. 让你瞬间萌比的35个python小技巧

    今天在看python算法的时候,看到一篇关于python的小技巧.瞬间萌比了,原来python也可以这样玩,太神奇了.萌比的是原来这么简单的东西自己都不知道,虽然会写.废话不多说了,开始上菜. 1.拆 ...

  7. 两个类似的ViewModel一个可以重写事件,另一个不能重写事件,是哪里出了错。

    答:继承错了,BaseViewModel里面是事件.

  8. SQL Server 一句Sql把表结构全部查询出来

    --一句Sql把表结构全部查询出来 SELECT 表名 = Case When A.colorder=1 Then D.name Else '' End, 表说明 = Case When A.colo ...

  9. spring整合hibernate时报错:org&period;hibernte&period;engine&period;transaction&period;spi&period;transactioncontext

    错误提示:Caused by:java.lang.ClassNotFoundException: org.hibernte.engine.transaction.spi.transactioncont ...

  10. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream&gt ...