【PAT】1098. 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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行给定一个正整数N(<=100)。接着下一行,给你N个数字代表初始数列。最后一行包括N个部分排序的数字。假设目标数列永远是升序排列的。一行内的所有的数字用空格隔开。

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.

翻译:对于每组输入数据,打印一行 “Insertion Sort” 或 "Heap Sort"来表明得到的部分结果的使用排序算法。接着运行下一次该方法的迭代并在第二行输出结果数列。数据保证每个测试数据结果唯一。一行内的所有的数字需要用空格隔开,并且在行的末尾不能有多余的空格。


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


解题思路

通过插入排序前面部分有序,后面部分与原数列相同来区分插入排序和堆排序,然后模拟堆排序。注意3 4 5 1 2 与3 4 5 1 2中, 排序后的数列与原数列相同,所以先找出插排的排序点,再判断后面部分相同,顺序不能颠倒。详见代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
#define bug puts("Hello\n")
using namespace std;
int n; 
int before[110];
int after[110];
int sorted[110];

int main(){
	scanf("%d",&n); 
	for(int i=0;i<n;i++) {
		scanf("%d",&before[i]);
		sorted[i]=before[i];
	}
	for(int i=0;i<n;i++) scanf("%d",&after[i]);
	int count1=0,count2=0;
	for(int i=0;i<=n;i++){
		if(after[i]>after[i+1]){
			count1=i;
			break;
		}
	}
	for(int i=count1+1;i<n;i++){
		if(before[i]==after[i]){
			count2++;
		}else break;
	}
	if(count2+count1+1==n){
		printf("Insertion Sort\n");
		sort(after,after+count1+2);
		for(int i=0;i<n-1;i++)
			printf("%d ",after[i]); 
		printf("%d\n",after[n-1]);
	}
	else{
		printf("Heap Sort\n");
		int a=n-1;
		while(after[a]>after[0]) a--;
		swap(after[a],after[0]);
		int low=0,high=1;
		while(high<a){
			if(high+1<a&&after[high]<after[high+1])high++;
			if(after[low]>after[high])break;
			swap(after[low],after[high]);
			low=high;high=low*2+1;
		}
		for(int i=0;i<n-1;i++)
			printf("%d ",after[i]); 
		printf("%d\n",after[n-1]);
	}
	return 0;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值