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

这道题和上一题差不多,稍微修改一点就好了。

09-排序3 Insertion or Heap Sort(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.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

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 "Heap 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 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9
AC代码如下,超级像的啦,我函数都直接拷贝过来的XDD

//
//  main.c
//  Insertion or Heap
//
//  Created by air on 2018/5/12.
//  Copyright © 2018年 air. All rights reserved.
//
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
void Insertion_sort_Compare(ElementType A[], int N, ElementType Result[]);
void print(ElementType A[], int N);
bool Compare(ElementType tmpResult[], ElementType Result[], int length);
void Heap_sort_Compare(ElementType A[], int total,ElementType Result[]);
void PercolateDown(ElementType A[], int i, int N);
void myswap(ElementType *a, ElementType *b);

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);
    Heap_sort_Compare(A,total,Result);
    return 0;
}
/*----------------------------------------------------------------------
 Heap_sort_compare, Build MaxHeap and then HeapSort
 -----------------------------------------------------------------------*/
void PercolateDown(ElementType A[], int i, int N){
    int parent, child;
    ElementType tmp = A[i];
    for(parent = i; parent * 2 + 1 < N; parent = child){
        child = parent * 2 + 1;
        if(child+1 <= N-1 && A[child+1] > A[child])
            child++;
        if(A[child] < tmp)
            break;
        A[parent] = A[child];
    }
    A[parent] = tmp;
}

void Heap_sort_Compare(ElementType A[], int total,ElementType Result[]){
    int i, j, flag;
    flag = 0;
    for(i = total/2 -1; i >= 0; i--){
        PercolateDown(A, i ,total);
    }
    for(j = total-1; j > 0; j--){
        myswap(&A[0], &A[j]);
        PercolateDown(A, 0, j);
        if(Compare(A, Result, total)){
            flag = 1;
            continue;
        }
        if(flag == 1){
            printf("Heap Sort\n");
            print(A, total);
            break;
        }
    }
}
void myswap(ElementType *a, ElementType *b){
    ElementType tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}
/*----------------------------------------------------------------------
 以下的插入排序正确,无需修改,只需要注意插入排序的第二个循环的判断条件就好了,一开始写错了
 -----------------------------------------------------------------------*/
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/80290603
今日推荐