The point is to sort each step to determine if it is the same as the second set of data, and if so, the output sort method and the next sequence.
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 Li St, and inserts it there. It repeats until no input elements remain.
Merge sort works as follows:divide the unsorted list into N sublists with each containing 1 element (a list of 1 ele ment is considered sorted). Then repeatedly merge, adjacent sublists to produce new sorted sublists until there are only 1 sublist remaining.
Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorti Ng method, can you tell which sorting method we is using?
Input Specification:
Each input file contains the one test case. For each case, the first line gives a positive integerN(≤< Span class= "Mord mathrm" >100). Then in the next Line, n Integers is given as the initial sequence. The last line contains the partially sorted sequence of The n numbers. It is assumed, the target sequence is always ascending. All the numbers in a line is 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, the answer is unique for each test case. All the numbers in a line must is separated by a space, and there must is no extra space at the end of the line.
Sample Input 1:
103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0
Sample Output 1:
Insertion Sort1 2 3 5 7 8 9 4 6 0
Sample Input 2:
103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6
Sample Output 2:
Merge Sort1 2 3 8 4 5 7 9 0 6
Chinese title:
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.
#include <iostream>#include<cstdio>#include<cstdlib>using namespaceStd;typedefintElementType;BOOLJudge (intOrigin[],intChanged[],intLen) { for(inti =0; i < Len; i++) { if(Origin[i]! =Changed[i])return false; } return true; } //Insert sort (need to sort array, array length, sort count)voidIsinsertionsort (ElementType origin[],intNintTimes ) { inti; ElementType Temp= Origin[times];//Take out the first element in an unordered sequence for(i = times; i >0&& origin[i-1] > temp; i--) Origin[i]= origin[i-1];//Compare and move the elements in the sorted sequence to the rightOrigin[i] =temp;}/*L = left start position, R = right start position, Rightend = right end position*/voidMerge (ElementType a[], ElementType tmpa[],intLintRintrightend) { /*merge ordered A[l]~a[r-1] and A[r]~a[rightend] into an ordered sequence*/ intLeftend = R-1;/*left End Position*/ inttemp = L;/*starting position of ordered sequence*/ intnumelements = Rightend-l +1; while(L <= leftend && R <=rightend) { if(A[l] <=A[r]) Tmpa[temp+ +] = a[l++];/*copy left element to Tmpa*/ Elsetmpa[temp+ +] = a[r++];/*Copy the right element to the Tmpa*/ } while(L <=leftend) Tmpa[temp+ +] = a[l++];/*copy directly to the left.*/ while(R <=rightend) Tmpa[temp+ +] = a[r++];/*copy directly to the left side*/ for(inti =0; i < numelements; i++, Rightend--) A[rightend]= Tmpa[rightend];/*copy the ordered tmpa[] back to a[]*/}/*length = size of the current ordered child column*//*22 merging adjacent ordered sub-columns*/voidMerge_pass (ElementType a[], ElementType tmpa[],intNintlength) { intI, J; for(i =0; I <= N-2*length; i + =2*length) Merge (A, Tmpa, I, I+length, i+2*length-1 ); if(I+length < N)/*Merge the last 2 sub-columns*/Merge (A, Tmpa, I, I+length, N-1); Else /*Finally, there are only 1 child columns left .*/ for(j = i; J < N; J + +) TMPA[J] =a[j];} voidMerge_sort (ElementType a[],intNintchanged[]) { intlength; ElementType*Tmpa; Length=1;/*Initialize the length of a sub-sequence*/Tmpa= (ElementType *)mallocNsizeof(ElementType)); if(Tmpa! =NULL) { while(Length <N) {merge_pass (A, Tmpa, N, length); if(Judge (Tmpa,changed,n)) {//is merge sortprintf"Merge sort\n"); Length*=2; Merge_pass (Tmpa, A, N, length); for(inti =0; I < n1; i++)//print once again sorted arrayprintf"%d", A[i]); printf ("%d\n", a[n-1]); return; } length*=2; Merge_pass (Tmpa, A, N, length); if(Judge (tmpa,changed,n)) {printf ("Merge sort\n"); Length*=2; Merge_pass (A, Tmpa, N, length); for(inti =0; I < n1; i++)//print once again sorted arrayprintf"%d", Tmpa[i]); printf ("%d\n", tmpa[n-1]); return; } length*=2; } Free(TMPA); } Elseprintf"Not enough space \ n" );}intMain () {intN; intorigin[ the],origin2[ the],changed[ the]; scanf ("%d", &N); for(inti =0; i < N; i++) {//origin origin1 Initial sequencescanf"%d",&Origin[i]); Origin2[i]=Origin[i]; } for(inti =0; i < N; i++)//changed sorted post sequencescanf"%d",&Changed[i]); for(inti =1; i < N; i++) {Isinsertionsort (Origin, N, i); if(Judge (Origin, Changed,n)) {//is the Insert sortprintf"insertion sort\n"); Isinsertionsort (origin, N, I+1); for(intj =0; J < N-1; J + +) printf ("%d", Origin[j]); printf ("%d\n", origin[n-1]); return 0; }} merge_sort (Origin2, N, changed); return 0;}
09-Sort 2 Insert or Merge