前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >09-排序2 Insert or Merge (25分)

09-排序2 Insert or Merge (25分)

作者头像
AI那点小事
发布2020-04-18 20:17:23
3460
发布2020-04-18 20:17:23
举报
文章被收录于专栏:AI那点小事AI那点小事

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 NN (\le 100≤100). Then in the next line, NN integers are given as the initial sequence. The last line contains the partially sorted sequence of the NN 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


AC代码:

代码语言:javascript
复制
#include <iostream>
using namespace std;

bool Check(int a[],int b[],int N)
{
    bool flag = true;
    for ( int i = 0 ; i < N ; i++){
        if ( a[i] != b[i]){
            flag = false;
            break;
        }
    }
    return flag;
}

void Print(int a[],int N)
{
    cout<<a[0];
    for (int i = 1 ; i < N ; i++){
        cout<<" "<<a[i];
    }
}

bool Insert_Sort(int a[],int b[],int N)
{
    bool isInsert = false;
    for ( int i = 1 ; i < N ; i++){
        int tmp = a[i];
        int j;
        for (j = i ; j > 0 && a[j-1] > tmp ; j--){
            a[j] = a[j-1];
        }
        a[j] = tmp;
        if (isInsert){
            cout<<"Insertion Sort"<<endl;
            Print(a,N);
            break;
        }else if(Check(a,b,N)){
            isInsert = true;    
        }
    }
    return isInsert;
}

void Merge(int a[],int left ,int right , int rightend)
{
    int tmp[rightend-left+1];
    int leftend = right-1;
    int len = rightend - left +1;
    int i = left , j = right ,k = 0;
    while(i <= leftend && j <= rightend){
        if( a[i] <= a[j]){
            tmp[k++] = a[i++];
        }else{
            tmp[k++] = a[j++];
        }
    }

    while(i <= leftend){
        tmp[k++] = a[i++];
    }
    while(j <= rightend){
        tmp[k++] = a[j++];
    }

    for(i = left, k = 0; i <= rightend; i++){
        a[i] = tmp[k++];
    }
}

bool Merge_Sort(int a[],int b[],int N)
{
    bool isMerge = false;
    for ( int i = 1 ; i < N ; i *= 2){
        for ( int j = 0 ; j < N ; j += i*2){
            if ( j + i*2 <= N){
                Merge(a,j,j+i,j+i*2-1);
            }else if( j + i <= N) {
                Merge(a,j,j+i,N-1);
            }
        }
        if (isMerge){
            cout<<"Merge Sort"<<endl;
            Print(a,N);
            break;
        }else if (Check(a,b,N)){
            isMerge = true;
        }
    }
    return isMerge;
}

int main()
{
    int len;
    cin>>len;
    int *A,*B,*C;
    A = new int[len];
    B = new int[len];
    C = new int[len]; 
    for ( int i = 0 ; i < len ; i++){
        cin>>A[i];
        C[i] = A[i];
    }
    for ( int i = 0 ; i < len ; i++){
        cin>>B[i];
    }

    bool isMerge = Merge_Sort(A,B,len);
    if ( isMerge){
        return 0;
    }else{
        bool isInsert = Insert_Sort(C,B,len);
        return 0;
    }

    return 0;
 } 

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档