排序碎碎念(一):数据结构基础— Insert or Merge

09-排序2 Insert or Merge(25 分)

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 (100). 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

//
//  main.c
//  Insert or Merge
//
//  Created by air on 2018/5/11.
//  Copyright © 2018年 air. All rights reserved.
/*------------------------------------
 归并排序一般用在外排序,内排序一般不用
 ------------------------------------*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
void Merge_pass(ElementType A[], ElementType TmpA[], int N, int length);
void Merge1(ElementType A[],ElementType TmpA[], int Lpos, int Rpos, int RightEnd);
void Merge_sort_compare(ElementType A[], int N,ElementType Result[]);
void Insertion_sort_Compare(ElementType A[], int N, ElementType Result[]);
void print(ElementType A[], int N);
bool Compare(ElementType tmpResult[], ElementType Result[], int length);

int main(int argc, const char * argv[]) {
    int total;
    scanf("%d",&total);
    ElementType * A;
    A = (ElementType *) malloc(sizeof(ElementType) * total);
    for(int i = 0; i < total; i++)
        scanf("%d",&A[i]);
    
    ElementType * Result;
    Result = (ElementType *)malloc(sizeof(ElementType) * total);
    for(int j = 0; j < total; j++)
        scanf("%d",&Result[j]);
    
    ElementType * tmpResult;
    tmpResult = (ElementType *) malloc(sizeof(ElementType) * total);
    for(int i = 0; i < total; i++)
        tmpResult[i] = A[i];

    Insertion_sort_Compare(tmpResult, total, Result);
    Merge_sort_compare(A, total,Result);
    
    return 0;
}

/*-----------------------------------------------------------
 非递归的Merge_sort
-------------------------------------------------------------*/
void Merge_sort_compare(ElementType A[], int N, ElementType Result[]){
    ElementType *tmpA;
    int length = 1;
    int flagOfTmp = 0;
    int flagOfA = 0;
    tmpA = (ElementType *)malloc(sizeof(ElementType) *(N));
    if(tmpA != NULL){
        while(length < N){
            Merge_pass(A, tmpA, N, length);
            length *= 2;
         
            if(flagOfA == 1){
                printf("Merge Sort\n");
                print(tmpA, N);
                break;
            }
            if(Compare(tmpA, Result, N)){
                flagOfTmp = 1;
            }
            
            Merge_pass(tmpA, A, N, length);
            length *= 2;
            
            if(flagOfTmp == 1){
                printf("Merge Sort\n");
                print(A, N);
                break;
            }
            if(Compare(A, Result, N)){
                flagOfA = 1;
            }
        }
        free(tmpA);
    }
}

void Merge_pass(ElementType A[], ElementType tmpA[], int N, int length){
    int i,j;
    for(i = 0; i <= N-2*length; i += 2*length){
        Merge1(A, tmpA, i, i+length, i+2*length - 1);
    }
    if(i + length < N)
        Merge1(A,tmpA, i, i+length, N-1);
    else{
        for(j = i; j < N; j++)
            tmpA[j] = A[j];
    }
}

void Merge1(ElementType A[],ElementType TmpA[], int Lpos, int Rpos, int RightEnd){
    int LeftEnd,NumElements, Tmpos;
    LeftEnd = Rpos - 1;
    Tmpos = Lpos;
    NumElements = RightEnd - Lpos + 1;
    while(Lpos <= LeftEnd && Rpos <= RightEnd){
        if(A[Lpos] <= A[Rpos])
            TmpA[Tmpos++] = A[Lpos++];
        else
            TmpA[Tmpos++] = A[Rpos++];
    }
    while(Lpos <= LeftEnd)
        TmpA[Tmpos++] = A[Lpos++];
    while(Rpos <= RightEnd)
        TmpA[Tmpos++] = A[Rpos++];
}
/*----------------------------------------------------------------------
 以下的插入排序正确,无需修改,只需要注意插入排序的第二个循环的判断条件就好了,一开始写错了
 -----------------------------------------------------------------------*/
void Insertion_sort_Compare(ElementType A[], int N, ElementType Result[]){
    int i,j;
    int tmp,flag = 0;
    for(i = 1; i < N; i++){
        tmp = A[i];
        for(j = i; j > 0 && A[j-1] > tmp; j--){  //POINT!!!!
            A[j] = A[j-1];
        }
        A[j] = tmp;
        if(flag == 1){
            print(A, N);
            break;
        }
        //每循环一次的时候就去比较一次
        if(Compare(A, Result, N)){
            printf("Insertion Sort\n");
            flag = 1;
            continue;
        }
    }
}
/*----------------------------------------------------------------------
 Comapre and Print
 -----------------------------------------------------------------------*/
bool Compare(ElementType tmpResult[], ElementType Result[], int length){
    for(int i = 0; i < length; i++){
        if(tmpResult[i] != Result[i])
            return false;
    }
    return true;
}

void print(ElementType A[], int N){
    int i;
    for(i = 0; i < N-1; i++)
        printf("%d ", A[i]);
    printf("%d\n",A[N-1]);
}

猜你喜欢

转载自blog.csdn.net/qq_25175067/article/details/80283933