PTA 09-排序2 Insert or Merge

PTA 09-排序2 Insert or Merge


原题链接

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, N integers 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
结尾无空行

题意

首先,搞清题意,我读题愣是没读明白。
第一行:N个元素的N
第二行:插入/归并排序的初始序列
第三行:插入/归并排序在排序过程中的某一步
输出:插入/归并排序在排序过程中的下一步结果

思路

1.sort()模拟插入排序
2.利用sort+step模拟归并排序

注意

1.插入排序得从2开始(第三个元素开始)

//返回i否则返回-1
int isInsert(int Insert[],int Target[],int N){
    for(int i=**2**;i<N;i++){
        sort(Insert,Insert+i);
        if(Match(Insert,Target,N)){
            return i;
        }
    }
    return -1;
}

否则报错:
在这里插入图片描述
原因:N<=2时,分不清是插入还是归并排序,因为过程一样

2.模拟Merge时,sort的小细节
最后一个子序列有可能是不足这个step长度的,因此要利用j来记录尾部长度
如果step(i+1)即按step进行排序的尾结点<=N,说明这段子序列在序列中是完整的:
j=step*(i+1);
否则,说明这个子序列是最后一个子序列,并且这个子序列的长度不足step:
j=N;

	int j;//标记结尾
    while(step<N){
        for(int i=0;i<=N/step;i++){
            if(step*(i+1)<=N){
                j=step*(i+1);
            }else{
                j=N;
            }
            sort(Merge+i*step,Merge+j);
            if(Match(Merge,Target,N)){
                return step;
            }
        }
        step*=2;
    }

Code

#include<iostream>
#include<algorithm>
using namespace std;
//判断两个数组是否完全相同
bool Match(int A[],int B[],int N){
    for(int i=0;i<N;i++){
        if(A[i]!=B[i]){
            return false;
        }
    }
    return true;
}
//返回i否则返回-1
int isInsert(int Insert[],int Target[],int N){
    for(int i=2;i<N;i++){
        sort(Insert,Insert+i);
        if(Match(Insert,Target,N)){
            return i;
        }
    }
    return -1;
}
//是mergesort的话返回step,否则-1
int isMergesort(int Merge[],int Target[],int N){
    int step=2;
    int j;//标记结尾
    while(step<N){
        for(int i=0;i<=N/step;i++){
            if(step*(i+1)<=N){
                j=step*(i+1);
            }else{
                j=N;
            }
            sort(Merge+i*step,Merge+j);
            if(Match(Merge,Target,N)){
                return step;
            }
        }
        step*=2;
    }
    return -1;
}
//打印最终结果
void printsort(int A[],int N){
    printf("%d",A[0]);
    for(int i=1;i<N;i++){
        printf(" %d",A[i]);
    }
    printf("\n");
}
int main(){
    int N;
    scanf("%d",&N);
    int Target[N],Insert[N],Merge[N];
    for(int i=0;i<N;i++){
        scanf("%d",&Insert[i]);//记录初始化状态
        Merge[i]=Insert[i];//记录初始化状态
    }
    for(int i=0;i<N;i++){
        scanf("%d",&Target[i]);//记录中间状态
    }
    int tempi=isInsert(Insert,Target,N);
    int Mergei=isMergesort(Merge,Target,N);
    if(tempi!=-1){
        printf("Insertion Sort\n");
        sort(Insert,Insert+tempi+1);
        printsort(Insert,N);
    }
    else if(Mergei!=-1){
        printf("Merge Sort\n");
        int step=Mergei;
        step*=2;
        int j;
        for(int i=0;i<=N/step;i++){
            if(step*(i+1)<=N){
                j=step*(i+1);
            }else{
                j=N;
            }
            sort(Merge+i*step,Merge+j);
        }
        printsort(Merge,N);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值