中国大学MOOC-陈越、何钦铭-数据结构
09-排序2 Insert or Merge (25 分)
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 N (≤100). Then in the next line, Nintegers are given as the initial sequence. The last line contains the partially sorted sequence of the N 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
算法思想:
其实这道题就是PAT乙级B1035,但测试点数据要比1035要复杂,算法思想请移步:https://blog.csdn.net/qq_38975493/article/details/89817668
下面给出两份代码,一份是用sort函数模拟Insert和Merge做的,一份是用Insert和Merge两个排序算法函数做的。
用sort函数模拟Insert和Merge:
/***************2019.5.14-09:00-11:20(V1.0) 11:20-11:37(V2.0)***********/
//09-排序2 Insert or Merge V2.0 用sort函数
#include<stdio.h>
#include<algorithm>
using namespace std;
int Match(int A[],int B[],int N){
int i;
for(i=0;i<N;i++){
if(A[i]!=B[i]) return 0;
}
return 1;
}
int main(){
int i;
int N;
scanf("%d",&N);
int Sorted[N];//partial sorted sequence
int Insert[N];//Insertion sort sequence
int Merge[N];//Merge sort sequence
for(i=0;i<N;i++){
scanf("%d",&Insert[i]);
Merge[i]=Insert[i];
}
for(i=0;i<N;i++){
scanf("%d",&Sorted[i]);
}
int step1=2,step2=2;//步伐选择十分重要!!!!step1=1是不能通过测试点2!!!
//19.7.25更新,插入排序步长初始化为2,是因为插入排序开始时就默认第一个元素有序,从第二个元素开始第一次排序
int flag=0;//0:Insertion,1:Merge
while(step1<N){
flag=0;
sort(Insert,Insert+step1);
step1++;
if(Match(Insert,Sorted,N)) break;
flag=1;
int n=N/step2;//number of mergers
if(N%step2>0) n++;
for(i=0;i<n;i++){
int l=i*step2;
int r=(i+1)*step2;
if(r>N) r=N;
sort(Merge+l,Merge+r);
}
step2*=2;
if(Match(Merge,Sorted,N)) break;
}
if(flag==0){
printf("Insertion Sort\n");
sort(Sorted,Sorted+step1);
}
else{
printf("Merge Sort\n");
int n=N/step2;//number of mergers
if(N%step2>0) n++;
for(i=0;i<n;i++){
int l=i*step2;
int r=(i+1)*step2;
if((i+1)*step2>N) r=N;
sort(Sorted+l,Sorted+r);
}
}
for(i=0;i<N;i++){
printf("%d",Sorted[i]);
if(i<N-1) printf(" ");
else printf("\n");
}
return 0;
}
用Insert和Merge两个排序算法函数:
/***************2019.5.14-09:00-11:20***********/
//09-排序2 Insert or Merge(V.10) //用InsertSort函数和MergeSort函数
#include<stdio.h>
int Match(int A[],int B[],int N){
int i;
for(i=0;i<N;i++){
if(A[i]!=B[i]) return 0;
}
return 1;
}
void InsertSort(int Insert[],int m,int N){
int i;
int tmp=Insert[m];
for(i=m;i>0&&tmp<Insert[i-1];i--){
Insert[i]=Insert[i-1];
}
Insert[i]=tmp;
}
void MergeSort(int Merge[],int step,int N){
if(step>N) return;//不能判step==N,特殊情况初始条件N==2!
int i,j,k,tmp;
int n=N/step;//number of mergers
if(N%step>0) n++;
for(i=0;i<n;i++){
int l=i*step;
int r=(i+1)*step-1;
if(r>=N) r=N-1;
for(j=l+1;j<=r;j++){
tmp=Merge[j];
for(k=j;k>l&&Merge[k-1]>tmp;k--){
Merge[k]=Merge[k-1];
}
Merge[k]=tmp;
}
}
}
int main(){
int i;
int N;
scanf("%d",&N);
int Sorted[N];//partial sorted sequence
int Insert[N];//Insertion sort sequence
int Merge[N];//Merge sort sequence
for(i=0;i<N;i++){
scanf("%d",&Insert[i]);
Merge[i]=Insert[i];
}
for(i=0;i<N;i++){
scanf("%d",&Sorted[i]);
}
int step1=1,step2=2;
int flag=0;//0:Insertion,1:Merge
while(step1<N){
flag=0;
InsertSort(Insert,step1,N);
step1++;
if(Match(Insert,Sorted,N)) break;
flag=1;
MergeSort(Merge,step2,N);
step2*=2;
if(Match(Merge,Sorted,N)) break;
}
if(flag==0){
printf("Insertion Sort\n");
InsertSort(Sorted,step1,N);
}
else{
printf("Merge Sort\n");
MergeSort(Sorted,step2,N);
}
for(i=0;i<N;i++){
printf("%d",Sorted[i]);
if(i<N-1) printf(" ");
else printf("\n");
}
return 0;
}