09-排序2 Insert or Merge

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 list, and inserts it there. It repeats until no input elements remain.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are 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 resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

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

Sample Output 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

Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6

  1 #include <cstdio>
  2 #define MAXN 101
  3 #include <algorithm>
  4 using namespace std;
  5 
  6 
  7 int Targ[MAXN];
  8 
  9 
 10 bool isSame(int *a, int *b, int N);
 11 int InsertionSort(int* A, int N);
 12 int MergeSort(int *A, int N);
 13 void Output(int *A, int N);
 14 
 15 
 16 
 17 int main()
 18 {
 19     int N, flag;
 20     int Ori[MAXN];
 21     int Ori2[MAXN];
 22     
 23     scanf("%d", &N);
 24     for (int i=0; i<N; i++) {
 25         scanf( "%d", &Ori[i] );
 26     }
 27     for (int i=0; i<N; i++) {
 28         scanf( "%d", &Targ[i] );
 29     }
 30     for (int i=0; i<N; i++) {
 31         Ori2[i] = Ori[i];
 32     }
 33 
 34     flag = InsertionSort(Ori, N);
 35     if(!flag) MergeSort(Ori2, N);
 36     
 37 }
 38 int InsertionSort(int A[], int N)
 39 {
 40     int i, j, temp;
 41     int flag = 0;
 42     for (i=1; i<N; i++) {
 43         temp = A[i];
 44         for (j=i; j>0 && A[j-1] > temp; j--) {
 45             A[j] = A[j-1];
 46         }
 47         A[j] = temp;
 48         
 49         if (flag) {
 50             Output(A, N);
 51             break;
 52         }
 53         
 54         if (isSame(A, Targ, N)) {
 55             printf("Insertion Sort\n");
 56             flag = 1;
 57         }
 58     }
 59     return flag;
 60     
 61 }
 62     
 63 int MergeSort(int *A, int N)
 64 {
 65     int P, i;
 66     int flag = 0;
 67     for (P=2; P<=N; P*=2) {
 68         for (i=0; i<N; i+=P) {
 69             sort( A + i, A + min(N, i+P) );
 70         }
 71         
 72         if (flag) {
 73             Output(A, N);
 74             break;
 75         }
 76         if( isSame(A, Targ, N) ){
 77             printf("Merge Sort\n");
 78             flag = 1;
 79         }
 80     }
 81     return flag;
 82     
 83 }
 84 
 85 bool isSame(int *a, int *b, int N)
 86 {
 87     int i;
 88     for (i=0; i<N; i++) {
 89         if(a[i] != b[i]) break;
 90     }
 91     if (i == N) {
 92         return true;
 93     }
 94     else
 95         return false;
 96 }
 97 void Output(int *A, int N)
 98 {
 99     printf("%d", A[0]);
100     for (int i=1; i<N; i++) {
101         printf(" %d", A[i]);
102     }
103     printf("\n");
104 
#include <cstdio>
#define MAXN 101
#include <algorithm>
using namespace std;


int Targ[MAXN];


bool isSame(int *a, int *b, int N);
int InsertionSort(int* A, int N);
int MergeSort(int *A, int N);
void Output(int *A, int N);



int main()
{
    int N, flag;
    int Ori[MAXN];
    int Ori2[MAXN];
    
    scanf("%d", &N);
    for (int i=0; i<N; i++) {
        scanf( "%d", &Ori[i] );
    }
    for (int i=0; i<N; i++) {
        scanf( "%d", &Targ[i] );
    }
    for (int i=0; i<N; i++) {
        Ori2[i] = Ori[i];
    }

    flag = InsertionSort(Ori, N);
    if(!flag) MergeSort(Ori2, N);
    
}
int InsertionSort(int A[], int N)
{
    int i, j, temp;
    int flag = 0;
    for (i=1; i<N; i++) {
        temp = A[i];
        for (j=i; j>0 && A[j-1] > temp; j--) {
            A[j] = A[j-1];
        }
        A[j] = temp;
        
        if (flag) {
            Output(A, N);
            break;
        }
        
        if (isSame(A, Targ, N)) {
            printf("Insertion Sort\n");
            flag = 1;
        }
    }
    return flag;
    
}
    
int MergeSort(int *A, int N)
{
    int P, i;
    int flag = 0;
    for (P=1; P<N; P*=2) {
        for (i=0; i<N; i+=2*P) {
            sort( A + i, A + min(N, i+2*P) );
        }
        
        if (flag) {
            Output(A, N);
            break;
        }
        if( isSame(A, Targ, N) ){
            printf("Merge Sort\n");
            flag = 1;
        }
    }
    return flag;
    
}

bool isSame(int *a, int *b, int N)
{
    int i;
    for (i=0; i<N; i++) {
        if(a[i] != b[i]) break;
    }
    if (i == N) {
        return true;
    }
    else
        return false;
}
void Output(int *A, int N)
{
    printf("%d", A[0]);
    for (int i=1; i<N; i++) {
        printf(" %d", A[i]);
    }
    printf("\n");
}
View Code

总结:

1.最白痴的方法模拟了两种走法,每步比较 O(N^2)

2.测试点3通不过的原因是归并排序没写好。

  归并函数的细节

 

int MergeSort(int *A, int N)
{
    int P, i;
    int flag = 0;
    for (P=1; P<N; P*=2) {
        for (i=0; i<N; i+=2*P) {
            sort( A + i, A + min(N, i+2*P) );
        }
        
        if (flag) {
            Output(A, N);
            break;
        }
        if( isSame(A, Targ, N) ){
            printf("Merge Sort\n");
            flag = 1;
        }
    }
    return flag;
    
}

 

posted @ 2019-05-31 08:10  Acoccus  阅读(187)  评论(0编辑  收藏  举报