(c语言详解)09-排序3 Insertion or Heap Sort(详细解释)

本博文源于浙江大学《数据结构》,看起来这道题跟上一道题
(c语言详解)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.

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 resulting 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

文章翻译是这样的:
根据维基百科:

插入排序迭代,每次重复使用一个输入元素,并生成一个已排序的输出列表。在每次迭代中,插入排序从输入数据中删除一个元素,找到它在已排序列表中的位置,并将其插入其中。它不断重复,直到没有输入元素保留。

堆排序将其输入分为已排序区域和未排序区域,并通过提取最大的元素并将其移动到已排序区域来迭代地缩小未排序区域。它涉及到使用堆数据结构而不是线性时间搜索来查找最大值。

现在给定整数的初始序列,以及某个排序方法几次迭代的结果,你能说出我们使用的是哪种排序方法吗?

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数N(≤100)。然后在下一行,N个整数作为初始序列。最后一行包含N个数字的部分排序序列。假设目标序列总是升序的。一行中的所有数字都用空格隔开。

输出规范:

对于每个测试用例,在第一行打印“插入排序”或“堆排序”,以指示用于获取部分结果的方法。然后再次运行这个方法,并在第二行输出结果序列。保证每个测试用例的答案都是唯一的。一行中所有的数字必须用空格隔开,而且行尾不能有多余的空格。

破题关键

然后再次运行这个方法,并在第二行输出结果序列。
抓住题目中这些关键字,进行开始写代码,或者看看博主的代码,就会发现,一切不过是老师上课的代码的拼凑

源码附上

#include<stdio.h>
#include<stdbool.h>

#define MAXSIZE 100

int B[MAXSIZE];
int flag = 0;
bool IsSame(int A[], int B[], int N) {
    for (int i = 0; i < N; i++) {
        if (A[i] != B[i])
            return false;
    }
    return true;
}

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

void PercDown(int A[], int N, int p) {
    int parent, child;
    int temp = A[p];
    for (parent = p; 2 * parent + 1 < N; parent = child) {
        child = 2 * parent + 1;
        if (child + 1 < N && A[child + 1] > A[child])
            child++;
        if (temp < A[child])
            A[parent] = A[child];
        else
            break;
    }
    A[parent] = temp;
}


void Heap_Sort(int A[], int N) {
    for (int p = N / 2 - 1; p >= 0; p--)
        PercDown(A, N, p);
    for (int i = N - 1; i > 0; i--) {
        int temp = A[i];
        A[i] = A[0];
        A[0] = temp;

        PercDown(A, i, 0);

        if (IsSame(A, B, N)) {
            temp = A[--i];
            A[i] = A[0];
            A[0] = temp;
            PercDown(A, i, 0);

            printf("Heap Sort\n");
            Print(A, N);
            return;
        }
    }
}



void InsertSort(int A[],int N) {
	int temp,i,j;
	for(i=1;i<N;i++) {
		temp = A[i];
		for(j=i-1;j>=0 && A[j]> temp;j--)
			A[j + 1] = A[j];
		A[j+1] = temp;
		
		if(IsSame(A,B,N)) {
			temp = A[++i];
			for(j=i-1;j>=0 && A[j]> temp;j--)
				A[j + 1] = A[j];
			A[j+1] = temp;
            printf("Insertion Sort\n");
            Print(A, N);
			return ;
		}
	}
}
int main()
{
    int N, A[MAXSIZE];
    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 insert_A[MAXSIZE], heap_A[MAXSIZE];
    for (int i = 0; i < N; i++) {
        insert_A[i] = A[i];
        heap_A[i] = A[i];
    }

    InsertSort(insert_A, N);
    Heap_Sort(heap_A, N);

    return 0;

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值