zoukankan      html  css  js  c++  java
  • 1089. 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, 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 resulting 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
    
    判断是选择排序还是归并排序,先从头检查是否是升序,直到不是升序检查后面的序列是否和原序列对应位置元素相同,如果相同就是选择排序,如果不是就是归并排序,选择排序就不用说了把最近的一个元素插入前面排好的序列中,归并排序求出每一块的元素个数m,每2m个元素排一下序。
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    using namespace std;
    int s[100],t[100],n,l,m;
    int check()
    {
        for(int i = l + 1;i < n;i ++)
        {
            if(s[i] != t[i])return 0;
        }
        return 1;
    }
    int main()
    {
        cin>>n;
        for(int i = 0;i < n;i ++)
        {
            cin>>s[i];
        }
        for(int i = 0;i < n;i ++)
        {
            cin>>t[i];
        }
        for(int i = 0;i < n - 1;i ++)
        {
            if(t[i] > t[i + 1])
            {
                l = i;
                break;
            }
            else if(i == n - 2)l = n - 1;
        }
        if(!check())
        {
            cout<<"Merge Sort"<<endl;
            int d = 1;
            m = l + 1;
            for(int i = l + 1;i < n - 1;i ++)
            {
                if(t[i] > t[i + 1])
                {
                    if(m > d && d != 1)m = d;
                    d = 1;
                }
                else d ++;
            }
            m *= 2;
            for(int i = 0;i < n;i += m)
            {
                if(i + m <= n)sort(t + i,t + i + m);
                else sort(t + i,t + n);
            }
        }
        else
        {
            cout<<"Insertion Sort"<<endl;
            if(l < n - 1)
            {
                int d = t[l + 1],i;
                for(i = l;i >= 0;i --)
                {
                    if(t[i] > d)t[i + 1] = t[i];
                    else break;
                }
                t[i + 1] = d;
            }
        }
        cout<<t[0];
        for(int i = 1;i < n;i ++)
            cout<<' '<<t[i];
    }
  • 相关阅读:
    How to write perfect C code
    通过IEnumerable和IDisposable实现可暂停和取消的任务队列
    解决HubbleDotNet搜索引擎索引数据不全的问题
    桌面开发者的界面故事,该醒醒了
    你可能不知道的陷阱, IEnumerable接口
    程序和界面简洁化设计的思考
    创建多模块springcloud应用eureka server和client和消费端demo
    yml配置文件
    使用 properties 配置文件装配 bean 的方式
    eclipse 开发 spring 、 springboot项目调试时一直跳转到 SilentExitExceptionHandler.exitCurrentThread 方法
  • 原文地址:https://www.cnblogs.com/8023spz/p/8391935.html
Copyright © 2011-2022 走看看