Pat-b 1035. Insert and Merge

Source: Internet
Author: User

Topic content:

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
Thinking Analysis:

The general idea of this problem is to simulate the insertion and merge sort, and compare the results of each step separately. This complexity is actually relatively high.
Because the topic guarantees that the result can only be one of the insertions and merges. So the "intermediate series" is judged by the characteristics of the intermediate results of the insertion sort.
The feature of the insertion sort is that the first number of bits is maintained in ascending order, and when one falls, the bit is the same as the original sequence.
If the decision is insertion, then insert the bit data into the previously ordered sequence.
If the decision is merge, calculate the merge through the existing sequence states to the first step, then run one more step, output.

Code:
#include <stdio.h>//number A is not divisible by 2intFactor_two (intA) {returna%2? A:factor_two (A/2);}//Verify if it is an insert sort, yes returns the breakpoint positionintIs_ins (intA[],intB[],intN) {intI, flag =0; for(i =0; I < n; i++)if(Flag = =0&& B[i] > b[i+1])//Verify ascendingFlag = i+1;Else if(Flag! =0&& a[i]! = B[i])//Verify that the original sequence is consistent             Break;returni = = n? Flag:0;//all validation indicates that the insertion is satisfied, the output position is not output 0}//combine two series into onevoidMrgintA[],intB[],intMintN) {inti =0, j =0, k =0, C[m+n]; while(i! = m | | J! = N) {if(i = = m) c[k++] = b[j++];Else if(j = n) c[k++] = a[i++];Else if(A[i] > B[j]) c[k++] = b[j++];Elsec[k++] = a[i++]; } for(i =0; i < m+n; i++) * (a+i) = * (C+i);}//Next mergevoidMRG_STP (intB[],intN) {printf("Merge sort\n");inti =0, num =0, cd =0, nol[ -] = {1}; for(i =0; I < n1; i++, nol[num]++)//Record the length of all ascending sequences        if(B[i] > b[i+1]) num++;//The CD here refers to the number of conventions for all ascending sequence lengths previously recordedcd = nol[0]/Factor_two (nol[0]);//Because the sequence length generated by the merge must be a multiple of 2, this extracts the maximum number of 2 contained in the CD     for(inti =0; I < num-1&& CD >1; i++)if(NOL[I]%CD! =0) CD/=2, i--;//Because the entire sequence can be divided into equal length (except the last one) ascending sequence, the subsequence length of the current step must be an approximate number of all ascending sequence lengths     for(i =0; i+2*CD < n; i + =2*CD)//According to the length of the obtained child column, each of the two items are mergedMRG (B+i, B+I+CD, CD, CD);if(I+CD < N) mrg (B+i, B+I+CD, CD, N-I-CD);}//Next insertionvoidINS_STP (intB[],intNintK) {printf("Insertion sort\n");inti =0, temp = b[k]; for(i = k; i >0; i--)if(Temp < b[i-1]) B[i] = b[i-1];Else  Break; B[i] = temp;}intMain () {intK, N, a[ -] = {0}, b[ -] = {0};scanf("%d", &n); for(inti =0; I < n; i++)//Data entry        scanf("%d", a+i); for(inti =0; I < n; i++)scanf("%d", b+i);if(k = Is_ins (A, B, N)))//Judging is not insertionINS_STP (b, n, K);ElseMRG_STP (b, N);printf("%d", b[0]); for(intj =1; J < N; J + +)//Output        printf("%d", B[j]);return 0;}

Click here to enter the question page

Pat-b 1035. Insert and Merge

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.