Programming Ability Test學習 1035. 插入與歸併(25)

來源:互聯網
上載者:User

標籤:

1035. 插入與歸併(25)時間限制200 ms記憶體限制65536 kB代碼長度限制8000 B判題程式Standard作者CHEN, Yue

根據維基百科的定義:

插入排序是迭代演算法,逐一獲得輸入資料,逐步產生有序的輸出序列。每步迭代中,演算法從輸入序列中取出一元素,將之插入有序序列中正確的位置。如此迭代直到全部元素有序。

歸併排序進行如下迭代操作:首先將原始序列看成N個只包含1個元素的有序子序列,然後每次迭代歸併兩個相鄰的有序子序列,直到最後只剩下1個有序的序列。

現給定原始序列和由某排序演算法產生的中間序列,請你判斷該演算法究竟是哪種排序演算法?

輸入格式:

輸入在第一行給出正整數N (<=100);隨後一行給出原始序列的N個整數;最後一行給出由某排序演算法產生的中間序列。這裡假設排序的目標序列是升序。數字間以空格分隔。

輸出格式:

首先在第1行中輸出“Insertion Sort”表示插入排序、或“Merge Sort”表示歸併排序;然後在第2行中輸出用該排序演算法再迭代一輪的結果序列。題目保證每組測試的結果是唯一的。數字間以空格分隔,且行末不得有多餘空格。

 

輸入範例1:
103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0
輸出範例1:
Insertion Sort1 2 3 5 7 8 9 4 6 0
輸入範例2:
103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6
輸出範例2:
Merge Sort1 2 3 8 4 5 7 9 0 6

提交代碼

 

 

#include<iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<string>#include<algorithm> #define INFINITE 1000 using namespace std;int dd=0;//比較兩個數組是否相等 bool FunctionCom(int *a,int *b,int N){    int i=0;    while(a[i]==b[i])i++;    if(i<=(N-1))return false;//兩個數組不相等返回0     else return true;//相等返回1 } //插入排序void insert(int *a,int *b,int n){    int i,j,key;    int k=0;    for(i=1;i<n;i++)//控制需要插入的元素    {        key=a[i]; //key為要插入的元素        for(j=i;j>0 && a[j-1]>key;j--) //尋找要插入的位置,迴圈結束,則找到插入位置        {            a[j] = a[j-1]; //移動元素的位置.供要插入元素使用        }        a[j] = key; //插入需要插入的元素        if(k==1)        {            for(int u=0;u<n;u++){            cout<<a[u];            if(u==n-1)cout<<endl;            else  cout<<" ";            }            k=0;        }        if(FunctionCom(a,b,n)==1){            cout<<"Insertion Sort"<<endl;            k=1;            dd=1;        }    }} int main(){    int N;    scanf("%d",&N);    int *a=new int[N];    int *b=new int[N];    for(int i=0;i<N;i++)cin>>a[i];    for(int i=0;i<N;i++)cin>>b[i];    int *a1=new int[N];    int *b1=new int[N];    for(int i=0;i<N;i++){a1[i]=a[i];b1[i]=b[i];}    insert(a,b,N);    if(dd==1)        goto kobe;    else {    cout<<"Merge Sort"<<endl;     int k = 1;              int flag=1;         //用來標記是否歸併到 “中間序列”              while( flag )              {                      flag = 0;                      for(int i=0; i<N; i++ )                          if( a1[i]!=b1[i] )                              flag = 1;                      k*=2;                      for(int i=0; i<N/k; i++ )                          sort( a1+i*k, a1+(i+1)*k );                      for(int i=k*(N/k); i<N; i++ ) // 對 非偶數序列的“尾巴”進行排序                          sort( a1+k*(N/k), a1+N );              }                 for(int u=0;u<N;u++){            cout<<a1[u];            if(u==N-1)cout<<endl;            else  cout<<" ";            }    }    kobe:    //cout<<FunctionCom(a,b,10);    return 0;} 
View Code

 

Programming Ability Test學習 1035. 插入與歸併(25)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.