PAT 1088 1089. Insert or Merge (25) (sort AH)

Source: Internet
Author: User

Title Link: http://www.patest.cn/contests/pat-a-practise/1089



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, each of the containing 1 element (a list of 1 element 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 integer N (<=100). Then on 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 resulting 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
Test Instructions:

Give two sequences, the first one is the initial sequence, the second is a sequence in the process of sorting with some sort!

Let's determine which method of the second sequence is sorted, and output the next sequence of the second sequence sorted with this sort!

The code is as follows:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace Std;int a[147], b[147];int n;int isequal (int x) {for (int i = x+1; I <= n; i++) {if (A[i]! = B[i]) {return 0;}} return 1;} int SORT (int len) {for (int i = 1, i <= N; i+=len) {for (int j = i; J < n && J < i+len-1; J + +) {if (B[j] > B [J+1]) {return 0;}}} return 1;} int main () {while (~SCANF ("%d", &n)) {int i;for (i = 1; I <= n; i++) {scanf ("%d", &a[i]);} for (i = 1; I <= n; i++) {scanf ("%d", &b[i]);} for (i = 1; i < n; i++) {if (B[i] > B[i+1]) {break;}} if (IsEqual (i+1))//The following are equal {printf ("insertion sort\n"); Sort (b+1,b+i+1 + 1);p rintf ("%d", b[1]); for (i = 2; I <= n; i++) { printf ("%d", B[i]);} printf ("\ n");} else{printf ("Merge sort\n"); int len = 2;while (sort (len))//search for the current sub-interval length of the merge sort {len *=2;} for (i = 1; I <= n; i+=len) {if (I+len <= N) {sort (b+i,b+i+len);} Else{sort (b+i,b+n+1);}} printf ("%d", b[1]); for (i = 2; I <= n; i++) {printf ("%d", B[i]);} printf ("\ n");}} return 0;} /*103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6103 1 2 8 7 5 9 4 0 61 3 2 8 7 5 9 4 0 6*/ 


PAT 1088 1089. Insert or Merge (25) (sort AH)

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.