09-排序3 Insertion or Heap Sort


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 NNN (≤100\le 100100). Then in the next line, NNN integers are given as the initial sequence. The last line contains the partially sorted sequence of the NNN 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


#include <stdlib.h>
#include <stdio.h>
#define FULL 99999
/* 评测结果 时间 	结果 	得分 	题目 	编译器 	用时(ms) 	内存(MB) 	用户
2016-05-01 08:51 	答案正确 	25 	09-排序3 	gcc 	2 	1 	569985011
测试点结果 测试点 	结果 	得分/满分 	用时(ms) 	内存(MB) 	要点提示
测试点1 	答案正确 	7/7 	2 	1 	sample Ins 中间步骤,有不需要交换的元素
测试点2 	答案正确 	6/6 	1 	1 	sample Hp 一般情况
测试点3 	答案正确 	2/2 	1 	1 	最小N, Ins 第一步没变
测试点4 	答案正确 	2/2 	2 	1 	最小N, Hp第1步
测试点5 	答案正确 	4/4 	1 	1 	最大N,Ins
测试点6 	答案正确 	4/4 	1 	1 	最大N,Hp,后面x位没变化
*/
/*先逐步执行插入排序并比较。
如果插入排序不是,那么进入堆排序。
首先把读入的数据分成两段(已排序的和未排序的),利用插入排序的结果找到断点
对前半段未排序的执行判断是不是一个有效的最大堆,如果不是提前跳出,否则,执行一次排序并输出*/


int Insertion(int *insert,const int*order,const int len);//插入排序
int HeapSort(int*,int *,const int len);//堆排序
int isit(int*a,const int*order,const int len);
void print(int*a,int len);

int main()
{
	int n;
	scanf("%d",&n);
	int insert[101];
	insert[0]=FULL;
	int order[101];
	order[0]=FULL;
	int f;
	for(int i=0; i<n; i++)
		scanf("%d",&insert[i+1]);//为便于堆排序的比较,这里从1开始存储

	for(int i=0; i<n; i++)
		scanf("%d",&order[i+1]);

	if(!Insertion(&insert[1],&order[1],n))
		HeapSort(order,insert,n);
	return 0;
}


int HeapSort(int*heap,int *order,const int len)
{
//	print(heap,len+1);
//	print(order,len+1);
	int BreakPoint=0;
	for(int i=len;i>0;i--){
		if(heap[i]!=order[i]){
			BreakPoint=i;
//			printf("BreakPoint[%d]",i);
			break;
		}
	}
	int flag=1;
	for(int i=1;i<=BreakPoint/2;i++){
		if(heap[i]<heap[i*2]){
			flag=0;break;
		}
	}
	if(!flag)return 0;
	int temp=heap[BreakPoint];
	
	heap[BreakPoint]=heap[1];
//	print(&heap[1],len);
	int son=1;
	for(int i=1;i*2<BreakPoint;){
		son=i*2;
		if(i*2<BreakPoint-1)
		if(heap[i*2]<heap[i*2+1])son=i*2+1;
		if(heap[son]>temp){
			heap[i]=heap[son];
			i=son;
		}else break;
	}
	heap[son]=temp;
	printf("Heap Sort\n");
	print(&heap[1],len);
	return 0;
}

int Insertion(int *insert,const int*order,const int len)
{
	int flag=0;

	for(int i=0; i<len; i++)
	{
		if(insert[i]<insert[i-1])
		{
			int temp=insert[i];
			int j;
			for(j=i; j>0; j--)
			{
				if(insert[j-1]>temp)insert[j]=insert[j-1];
				else break;
			}
			insert[j]=temp;
			if(flag)break;
		}
		flag=isit(insert,order,len);
	}
	if(flag)
	{
		printf("Insertion Sort\n");
		print(insert,len);
		return 1;
	}
	return 0;
}


void print(int*a,int len)
{
	for(int i=0; i<len; i++)
	{
		if(i)printf(" ");
		printf("%d",a[i]);
	}
	printf("\n");
}

int isit(int*a,const int*order,const int len)
{

	for(int i=0; i<len; i++)
	{
		if(order[i]!=a[i])
		{
//			print(a,len);
//			printf("\n");
			return 0;
		}
	}
	return 1;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值