PAT (Basic level) practise (Chinese) 1035. Insert and Merge (25) __ Sort

Source: Internet
Author: User
Tags first row
1035. Insert and merge (+)Time limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Standard author CHEN, Yue

According to Wikipedia's definition:

The insertion sort is an iterative algorithm, in which the input data is obtained and the ordered output sequence is gradually produced. 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 the elements are ordered.

Merge Sort does the following iterative operations: First, the original sequence is considered to be an ordered sequence of n containing only 1 elements, and then each iteration merges two contiguous ordered Subsequence, until the last only 1 ordered sequences are left.

Given the original sequence and the intermediate sequence produced by a sort algorithm, you should determine what sort of algorithm the algorithm is.

Input Format:

The input gives a positive integer n (<=100) in the first row, then a row gives n integers of the original sequence, and the last line gives the intermediate sequence produced by a sort algorithm. This assumes that the ordered target sequence is ascending. Numbers are separated by spaces.

output format: First output "insertion sort" in line 1th indicates that the insertion sort, or "merge sort" represents the merge sort, and then outputs a sequence of results in line 2nd with the sorting algorithm to iterate over the next round. The title ensures that the results of each set of tests are unique. Numbers are separated by spaces, and no extra space is allowed at the end of the line.

Enter Sample 1:

3 1 2 8 7 5 9 4 6 0 1 2 3 7 8 5 9 4 6-0

Output Sample 1:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
Enter Sample 2:
3 1 2 8 7 5 9 4 0 6 1 3 2 8 5 7 4 9 0-6

Output Sample 2:
Merge Sort
1 2 3 8 4 5 7 9 0 6
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=105;
int A[MAXN],B[MAXN];

int main () {
    int n;
    scanf ("%d", &n);
    for (int i=0;i<n;i++) scanf ("%d", &a[i]);
    for (int i=0;i<n;i++) scanf ("%d", &b[i]);
    int i,j;
    for (i=0;i<n-1&&b[i]<=b[i+1];i++);
    for (j=i+1;j<n&&a[j]==b[j];j++);
    if (j==n) {
        printf ("Insertion sort\n");
        Sort (a,a+i+2);
    }
    else{
        printf ("Merge sort\n");
        int k=1,flag=1;
        while (flag) {
            flag=0;
            for (i=0;i<n;i++) {
                if (A[i]!=b[i]) flag=1
            }
            k*=2;
            for (i=0;i<n/k;i++) sort (a+i*k,a+ (i+1) *k);
            Sort (a+n/k*k,a+n);
        }
    }
    for (j=0;j<n;j++) {
        if (j!=0) printf ("");
        printf ("%d", a[j]);
    return 0;
}


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.