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];
    }
  • 相关阅读:
    6-MySQL-Ubuntu-操作数据表的基本操作(一)
    5-MySQL-Ubuntu-操作数据库的基本操作语句
    11-Ubuntu-根目录下各目录的功能详细介绍
    4-Ubuntu-启动/关闭/重启mysql服务
    3-Windows-CMD启动mysql服务-连接本地mysql服务-连接远程mysql服务
    2-Ubuntu命令安装mysql服务器和客户端及安装后的简单验证操作
    1-Navicat无法远程连接Ubuntu上的MySQL(已解决)
    10-python基础—索引与切片总结
    Useful Things To Know About Machine Learning (机器学习中一些有用的知识)
    How to Use the TimeDistributed Layer for Long Short-Term Memory Networks in Python 译文
  • 原文地址:https://www.cnblogs.com/8023spz/p/8391935.html
Copyright © 2011-2022 走看看