pat-basic-1035-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

Judgment sorting method: Take 2 as the block, determine whether the block is orderly . Because the solution is unique , if it satisfies the order within each block , then it can certainly be a step in the merge sort ! Conversely, if there is a block inside the disorder , it is necessary to insert sort
For the insertion sort , just find the unreasonable position pos, and start with the 0-pos bit sort, you can use the sort function directly or simulate it manually
For a merge sort , you first need to find the largest gap (1,2,4,8 ...). satisfies each block to be ordered, then sorts the data in each (2*GAP) block (any sort method); Merge sort requires attention to the processing of the last discontent (2*GAP)
#include <bits/stdc++.h>#defineLL Long Long#defineMAXN 100+50using namespacestd;intN;intARRNOTSORTED[MAXN];intARRSORTED[MAXN];BOOLInsertflag =false;BOOLcmpConst int&a,Const int&b) {    returnA <b;}intMain () {scanf ("%d", &N);  for(inti =0; I < n; ++i) {scanf ("%d", &Arrnotsorted[i]); }     for(inti =0; I < n; ++i) {scanf ("%d", &Arrsorted[i]); }     for(inti =0; I < n; i + =2){        if(i+1<N) {            if(Arrsorted[i] > arrsorted[i+1]) {Insertflag=true; }        }    }    if(insertflag) {//Insertprintf"insertion sort\n"); intPOS;  for(pos =0; POS < n-1; pos++){            if(Arrsorted[pos] > arrsorted[pos+1]){                 Break; }} POS++; intval =Arrsorted[pos]; //Justify POS         for(inti =0; I < POS; ++i) {            if(Arrsorted[i] >Arrsorted[pos]) {                //i->i+1 ... pos                 for(intj = Pos; J > i; --j) {Arrsorted[j]= arrsorted[j-1]; } Arrsorted[i]=Val;  Break; }        }         for(inti =0; I < n; ++i) {            if(i = =0) {printf ("%d", Arrsorted[i]); }            Else{printf ("%d", Arrsorted[i]); }        }    }    Else{printf ("Merge sort\n"); //Merge        intGap =4; BOOLFlag =true;  while(1){             for(intt =0; T < N/gap; ++t) {                if(!flag) {                     Break; }                 for(inti =0; I < gap-1; ++i) {                    if(Arrsorted[i+t*gap] > arrsorted[i+1+t*Gap]) {Flag=false;  Break; }                }            }            if(!flag) {                 Break; } Gap*=2; }         for(intt =0; T < N/gap; ++t) {Sort (arrsorted+t*gap, arrsorted+ (t+1)*Gap, CMP); }        if(n% Gap! =0) {sort (arrsorted+n/gap*gap, arrsorted+N, CMP); }         for(inti =0; I < n; ++i) {            if(i = =0) {printf ("%d", Arrsorted[i]); }            Else{printf ("%d", Arrsorted[i]); }        }    }    return 0;}
Capouis ' CODE

pat-basic-1035-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.