pat-B-1035. Insertion and Merging (25)

Source: Internet
Author: User

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

The code needs to be updated ...

1#include <iostream>2#include <vector>3#include <algorithm>4 using namespacestd;5 6 intMain ()7 {8     intN;9CIN >>N;Tenvector<int> A1 (n+1); Onevector<int> A2 (n+1); A     intidx1,idx2; -      for(inti =0; i < N; i++) -CIN >>A1[i]; the      for(inti =0; i < N; i++) -CIN >>A2[i]; -     //IDX as the last ordered sequence of the last element to roll out the loop -      for(idx1 =0; A2[IDX1] <= a2[idx1 +1] && idx1 < N-1; idx1++); +      for(idx2 = ++idx1; A1[IDX2] = a2[idx2] && idx2 < N; idx2++); -  +     if(idx2 = = N)//This allows you to determine whether this sort procedure is an insert sort A     { atcout <<"insertion Sort"<<Endl; -Sort (&a1[0], &a1[idx1+1]); -     } -     Else -     { -cout <<"Merge Sort"<<Endl; in         intK =1; -         BOOLFlag =true;//used to mark whether an intermediate sequence to          while(flag) +         { -Flag =false; the             if(!equal (A1.begin (), A1.end (), A2.begin ( ))) *Flag =true; $ Panax NotoginsengK *=2; -              for(inti =0; i < n/k; i++) the             { +Sort (&a1[i*k], &a1[(i +1)*K]); A             } the              for(inti = k* (n/k); i<n; i++) +             { -Sort (&a1[k* (n/k)],&a1[n]); $             } $         } -     } -cout << a1[0]; the      for(inti =1; i < N; i++) -cout <<" "<<A1[i];Wuyi  the     return 0; -}

pat-B-1035. Insertion and Merging (25)

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.