09-排序3 Insertion or Heap Sort

中国大学MOOC-陈越、何钦铭-数据结构

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, Nintegers 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

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

算法思想:

①练手题,注意不能取巧,只能逐步求排序序列然后作比较。

②(2019.8.20更新) = = 看算法②,可根据特性判是插入还是堆排

(能校准算法思想,像我之前建堆一样,没能遵循调堆规则,虽然结果正确,但是过程中打乱了堆的次序)

XJBG版堆排:https://blog.csdn.net/qq_38975493/article/details/90171052

算法① 

/***************2019.5.15-09:38-11:03**************/
//09-排序3 Insertion or Heap Sort
#include<stdio.h>
#include<algorithm>
using namespace std;

//*************
//InsertSort
//*************
void InsertSort(int Insert[],int step,int N){
	int i;
	int tmp=Insert[step];
	for(i=step;i>0&&tmp<Insert[i-1];i--) Insert[i]=Insert[i-1];
	Insert[i]=tmp;
}

//*************
//HeapSort
//*************
void Swap(int A[],int p,int q){
	int tmp=A[p];
	A[p]=A[q];
	A[q]=tmp;
}
void AdjustHeap(int Heap[],int p,int Last){
	int Parent,Child;
	int tmp=Heap[p];
	for(Parent=p;Parent*2+1<Last;Parent=Child){
		Child=Parent*2+1;
		if(Child+1<Last&&Heap[Child+1]>Heap[Child]) Child++;
		if(Heap[Child]>tmp) Heap[Parent]=Heap[Child];
		else break;
	} 
	Heap[Parent]=tmp;
} 
void BuildMaxHeap(int Heap[],int N){
	int i;
	for(i=(N-1)/2;i>=0;i--){ //从下到上调整每一课子树 
		AdjustHeap(Heap,i,N);
	}
//	for(i=0;i<N;i++) printf("%d ",Heap[i]);
}
void HeapSort(int Heap[],int step,int N){
	int Last=N-step;
	if(Last<=0) return;
	Swap(Heap,0,Last);
	AdjustHeap(Heap,0,Last);
} 


//*************
//Match
//*************
int Match(int A[],int Sorted[],int N){
	int i;
	for(i=0;i<N;i++){
		if(A[i]!=Sorted[i]) return 0;
	}
	return 1;
}

//*************
//main
//*************
int main(){
	int i;
	int N;
	scanf("%d",&N);
	int Sorted[N],Insert[N],Heap[N];
	for(i=0;i<N;i++){
		scanf("%d",&Insert[i]);
		Heap[i]=Insert[i];
	}
	for(i=0;i<N;i++) scanf("%d",&Sorted[i]);
	
	BuildMaxHeap(Heap,N); //需要先在比较前建起最大堆才能同步step 
    int step=1;
	int flag=0;//1:Insert,2:Heap
	while(step<N){
		InsertSort(Insert,step,N);
		if(Match(Insert,Sorted,N)){
			flag=1;
			break;
		} 
		else HeapSort(Heap,step,N);
		if(Match(Heap,Sorted,N)){
			flag=2;
			break;
		} 
		step++;
	}
	
	if(flag==1){
		printf("Insertion Sort\n");
		InsertSort(Sorted,step+1,N);
	}
	else if(flag==2){
		printf("Heap Sort\n");
		HeapSort(Sorted,step+1,N);
	}
	
	for(i=0;i<N;i++){
		printf("%d",Sorted[i]);
		if(i<N-1) printf(" ");
		else printf("\n");
	}
	return 0;
}

算法②:

/***************2019.8.20-15:20-16:51**************/
//A1098 Insertion or Heap Sort (25 分) 1.5h
//题目分析:
//算法思想:
//遇到的问题:
#include<iostream>
#include<algorithm>
using namespace std;
int pre[101],now[101];

void Heap(int last){
	int root=now[1];
	int parent=1,child;
	for(parent;parent*2<=last;parent=child){
		child=parent*2;
		if(child+1<=last&&now[child+1]>=now[child]){
			child++;
		} 
		if(now[child]>root) now[parent]=now[child];
		else break;
	}
	now[parent]=root;
}
int main(){
	int i,j;
	int n;
	scanf("%d",&n);
	for(i=1;i<=n;i++) scanf("%d",&pre[i]);
	for(i=1;i<=n;i++) scanf("%d",&now[i]);
	sort(pre+1,pre+n+1);
	int k=1,m=0,flag;
	for(i=2;i<=n;i++){
		if(now[i-1]<=now[i]) k++;
		else break;
	}
	if(k>1)	flag=1;//根据题目,前k个有序,必定是插入排序 
	else{
		for(i=n;i>=1;i--){
			if(now[i]==pre[i]) m++;
			else break;
		}
		if(m>0) flag=2;
		else flag=1;
	} 
	if(flag==1){
		printf("Insertion Sort\n");
		sort(now+1,now+2+k);
	} 
	else{
		printf("Heap Sort\n");
		swap(now[1],now[n-m]);
		Heap(n-m-1);
	}
	for(i=1;i<=n;i++){
		printf("%d",now[i]);
		if(i<n) printf(" ");
		else printf("\n");
	}
	return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值