zoukankan      html  css  js  c++  java
  • Insert or Merge

    7-13 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 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
     1 #include<iostream>
     2 using namespace std;
     3 void Merge(int sequence1[],int l,int r,int rend,int temp[]){
     4     int lend=r-1;
     5     int s=l;
     6     while(l<=lend&&r<=rend){
     7         if(sequence1[l]<sequence1[r]) temp[s++]=sequence1[l++];
     8         else temp[s++]=sequence1[r++];
     9     }
    10     while(l<=lend) temp[s++]=sequence1[l++];
    11     while(r<=rend) temp[s++]=sequence1[r++];
    12 }
    13 int main(){
    14     int N;
    15     int flag=1;//Insertion_sort
    16     cin>>N;
    17     int sequence[N];
    18     int sequence1[N];
    19     for(int i=0;i<N;i++)
    20     cin>>sequence[i];
    21     for(int i=0;i<N;i++)
    22     cin>>sequence1[i];
    23     int i,j;
    24     for(j=0;j<N-1;j++)
    25     if(sequence1[j]>sequence1[j+1]) break;
    26     int sign=j;
    27     for(j=j+1;j<N;j++)
    28     if(sequence[j]!=sequence1[j]) flag=0;//Merge_sort
    29     if(flag==1){
    30         cout<<"Insertion Sort"<<endl;
    31         int temp=sequence1[sign+1];
    32         for(i=sign+1;i>=0;i--)
    33         if(temp<sequence1[i-1]) 
    34         sequence1[i]=sequence1[i-1];
    35         else break;
    36         sequence1[i]=temp;
    37         int tag=0;
    38         for( i=0;i<N;i++){
    39             if(tag++==0) cout<<sequence1[i];
    40             else cout<<" "<<sequence1[i];
    41         }
    42     }
    43     else{
    44         int tag=1;
    45         cout<<"Merge Sort"<<endl;
    46         int length;
    47         for(length=2;length<=N;length*=2){
    48         for(i=length-1;i<N-1;i+=2*length)
    49         if(sequence1[i]>sequence1[i+1])
    50         {tag=0; break;}
    51         if(tag==0) break;
    52         }
    53         int temp[N];
    54         for(i=0;i<N-2*length;i+=2*length)
    55         Merge(sequence1,i,i+length,i+2*length-1,temp);
    56         if(i+length<N)
    57         Merge(sequence1,i,i+length,N-1,temp);
    58         else
    59         for(;i<N;i++) temp[i]=sequence[i];
    60         int tick=0;
    61         for(int k=0;k<N;k++)
    62         if(tick++==0) cout<<temp[k];
    63         else cout<<" "<<temp[k];
    64         cout<<endl;
    65     }
    66     return 0; 
    67 } 
    View Code
     

     

  • 相关阅读:
    pyecharts 0.5.11介绍
    Python并发写入一个文件
    subprocess模块详解(二)
    subprocess模块详解(一)
    大数据环境下的数据仓库建设
    Java计算字符串相似度
    Pandas matplotlib无法显示中文解决办法
    Hadoop YARN参数介绍(四)[推荐]
    Hadoop YARN参数介绍(三)
    wsl2 ubuntu20.04 上使用 kubeadm 创建一个单主集群
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8056351.html
Copyright © 2011-2022 走看看