pat1035--insertion and Merging

Source: Internet
Author: User

According to the definition of Wikipedia:

The insertion sort is an iterative algorithm, which obtains input data, and gradually produces an ordered output sequence. In each iteration, the algorithm extracts an element from the input sequence and inserts it into the correct position in the ordered sequence. So iterate until all elements are ordered.

The merge sort does the following: First, the original sequence is treated as an ordered sub-sequence of n 1 elements, and then each iteration merges two contiguous ordered sub-sequences until only 1 ordered sequences are left.

Now given the original sequence and the intermediate sequence produced by a sorting algorithm, would you please determine what sort algorithm it is?

Input format:

The input gives a positive integer n (<=100) on the first line, and then a row gives n integers of the original sequence, and the last line gives the intermediate sequence produced by a sort algorithm. Here it is assumed that the order of the target sequence is ascending. The numbers are separated by a space.

Output format:

First, the output "insertion sort" in line 1th indicates that the insertion sort, or "merge sort" means the merge sort, and then in line 2nd, outputs a sequence of results with the sorting algorithm that iterates round. The topic guarantees that the results of each set of tests are unique. The numbers are separated by a space, and no extra spaces are allowed at the end of the line.

Input Sample 1:

103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0

Output Example 1:

Insertion Sort1 2 3 5 7 8 9 4 6 0

Input Sample 2:

103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6

Output Example 2:

Merge Sort1 2 3 8 4 5 7 9 0 6
A test point has not been, do not know how to modify, with a speculative method, in the process of merging sorting with the sort ... If it is recursive it seems difficult to see the results of each iteration directly.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cctype>#include<cstdlib>#include<cmath>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#include<cctype>using namespacestd;typedef unsignedLong Longull;#defineINF 0XFFFFFFFints1[101],s2[101],a[101],flag,n;int  is(){    inti,j,k;  for(i=2; i<=n;++i) {if(a[i]<a[i-1]) {a[0]=A[i]; A[i]=a[i-1];  for(j=i-2; a[0]<a[j];--j) {A[j+1]=A[j]; } a[j+1]=a[0];  for(k=1; k<=n;++k) {if(a[k]!=S1[k]) Break; }            if(flag) { for(k=1; k<=n;++k) {S2[k]=A[k]; }                    return 1; }            if(k==n+1) Flag=1; }    }    return 0;}intMS () {inti,j,k;  for(i=1; i<=2*n;i*=2) {J=1;  while(1)            {                if(j+i>=N) {sort (a+j,a+n+1);  Break; } sort (A+j,a+j+i); J=j+i; }             for(k=1; k<=n;++k) {if(a[k]!=S1[k]) Break; }            if(flag) { for(k=1; k<=n;++k) {S2[k]=A[k]; }                    return 1; }            if(k==n+1) Flag=1; }    return 0;}intMain () {intX,y,i,j,k,ans;  while(cin>>N) {flag=0;  for(i=1; i<=n;++i) {cin>>S2[i]; }         for(i=1; i<=n;++i) {A[i]=S2[i]; }                 for(i=1; i<=n;++i) {cin>>S1[i]; }        if(MS ()) {cout<<"Merge Sort"<<Endl; cout<<s2[1];  for(i=2; i<=n;++i) cout<<" "<<S2[i]; cout<<Endl; }            Else        {             for(i=1; i<=n;++i) {A[i]=S2[i]; }             is(); cout<<"insertion Sort"<<Endl; cout<<s2[1];  for(i=2; i<=n;++i) cout<<" "<<S2[i]; cout<<Endl; }    }    return 0;}

pat1035--insertion and Merging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.