1035. Insert and Merge (25) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue
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
Submit Code
#include <iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#defineINFINITE 1000using namespacestd;intDd=0;//compares two arrays for equalityBOOLFunctioncom (int*a,int*b,intN) { intI=0; while(A[i]==b[i]) i++; if(I<= (n1))return false;//two arrays are not equal return 0 Else return true;//equal return 1} //Insert SortvoidInsertint*a,int*b,intN) { intI,j,key; intk=0; for(i=1; i<n;i++)//control which elements need to be inserted{Key=a[i];//key is the element to be inserted for(j=i;j>0&& a[j-1]>key;j--)//Find where to insert, loop ends, find insertion position{A[j]= a[j-1];//the position of the moving element. For inserting elements to use} A[j]= key;//Insert the element you want to insert if(k==1) { for(intu=0; u<n;u++) {cout<<A[u]; if(u==n-1) cout<<Endl; Elsecout<<" "; } k=0; } if(functioncom (a,b,n) = =1) {cout<<"insertion Sort"<<Endl; K=1; DD=1; } }} intMain () {intN; scanf ("%d",&N); int*a=New int[N]; int*b=New int[N]; for(intI=0; i<n;i++) cin>>A[i]; for(intI=0; i<n;i++) cin>>B[i]; int*a1=New int[N]; int*b1=New int[N]; for(intI=0; i<n;i++) {a1[i]=a[i];b1[i]=b[i];} Insert (A,B,N); if(dd==1) GotoKobe; Else{cout<<"Merge Sort"<<Endl; intK =1; intflag=1;//used to mark whether to merge into the "intermediate sequence" while(flag) {flag=0; for(intI=0; i<n; i++ ) if(a1[i]!=B1[i]) flag=1; K*=2; for(intI=0; i<n/k; i++) sort (A1+i*k, a1+ (i+1)*k); for(intI=k* (n/k); i<n; i++)//sort the "tails" of a non-even sequenceSort (a1+k* (n/k), a1+N); } for(intu=0; u<n;u++) {cout<<A1[u]; if(u==n-1) cout<<Endl; Elsecout<<" "; }} Kobe://cout<<functioncom (a,b,10); return 0;} View Code
Programming Ability Test study 1035. Insertion and Merging (25)