PAT B 1035 and insert merge

Subject description:

According to Wikipedia's definition:

  • Insertion sort is an iterative algorithm, to obtain input data one by one, gradually produce ordered output sequence. Each iteration, the algorithm an element taken from the input sequence, insert it into the correct position in the ordered sequence. So iteration until all the elements in order.
  • Merge sort the following iterative operation: First, as the original sequence of N ordered subsequences contains only one element, and each iteration of the ordered merge two adjacent sequences, only one until the last ordered the sequence of.

Now given the original sequence and middle sequence generated by a sorting algorithm, the algorithm determines whether you what kind of sorting algorithm?

Input formats:

In the first line of the input is given positive integer N (≦ 100); subsequently given row of the original sequence of N integers; last line gives the sequence generated by the intermediate of a sorting algorithm. It is assumed that the target sequence is sorted in ascending order. Between numbers separated by a space.

Output formats:

First output line 1Insertion SortIt represents insertion sort, orMerge SortIt represents a merge sort; then outputs the ranking algorithm iterate a sequence of results in the second row. Topic ensure each test result is unique. Between numbers separated by a space, and the line from beginning to end may not have the extra space.

Sample Input 1:

10
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

Sample Input 2:

10
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

analysis:

And that the need to master the non-recursive algorithm insertion sort and merge sort.
In order to get the next trip to the sort of the sort, I choose to join a flag marking the judge ordering, if the current pass of the sort given agreement, the flag is set true, then a trip to sort and then exit.

Code:

#include <cstdio>
#include <algorithm>
using namespace std;

//判断数组内容是否相同
bool isSame(int a[], int b[], int n){
    for(int i = 0; i < n; i++)
        if(a[i] != b[i])
            return false;
    return true;
}

//插入排序
bool insertSort(int a[], int b[], int n){
    int temp, j;
    //flag用来判断是否该次排序是否已经与test相同,若相同,则再进行一次排序,存在a中
    bool flag = false;
    int count = 0;
    for(int i = 1; i < n; i++){
        count++;
        temp = a[i];
        for(j = i; j > 0; j--){
            if(a[j - 1] > temp)
                a[j] = a[j - 1];
            else
                break;
        }
        a[j] = temp;
        if(isSame(a, b, n))
            flag = true;
        else if(flag)
            return true;
    }
    return flag;
}

//归并排序非递归
bool mergeSort(int a[], int b[], int n){
    int count = 1;
    //flag用来判断是否该次排序是否已经与test相同,若相同,则再进行一次排序,存在a中
    bool flag = false;
    while(count * 2 <= n){
        count *= 2;
        int left = 0, right = count - 1;
        while(right <= n - 1){
            sort(a + left, a + right + 1);
            if(right >= n - 1)
                break;
            left += count;
            right = right + count >= n - 1 ? n - 1 : right + count;

        }
        if(isSame(a, b, n))
            flag = true;
        else if(flag)
            return true;
    }
    return flag;
}

//输出数组
void printArray(int a[], int n){
    bool flag = false;
    for(int i = 0; i < n; i++){
        if(!flag){
            flag = true;
            printf("%d", a[i]);
        }
        else
            printf(" %d", a[i]);
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    int num[n], temp[n], test[n];   //num, temp均为初始数组,test为经过排序后的内容
    for(int i = 0; i < n; i++){
        scanf("%d", &num[i]);
        temp[i] = num[i];
    }
    for(int i = 0; i < n; i++)
        scanf("%d", &test[i]);
    bool isInsertSort = insertSort(temp, test, n);  //选择用插入排序判断是否为该方法
    if(!isInsertSort){
        //若不为插入排序,需要进行归并排序
        printf("Merge Sort\n");
        mergeSort(num, test, n);
        printArray(num, n);
    }
    else{
        //若为插入排序,则可直接输出传入的temp数组
        printf("Insertion Sort\n");
        printArray(temp, n);
    }
    return 0;
}

Published 10 original articles · won praise 0 · Views 114

Guess you like

Origin blog.csdn.net/qq_41421484/article/details/104010298