插入排序+堆排序 09-排序3 Insertion or Heap Sort (25分)

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

解题
写堆排序和拆入排序,每一步进行判断;

1.输入函数
为了方便,T用于插入排序判断,I用于堆排序判断;

#include<iostream>
using namespace std;
#define MAXN 101

int N;
int T[MAXN]; 
int I[MAXN];
int Result[MAXN];
void input()
{
	cin>>N;
	for(int i=0;i<N;i++)
	cin>>T[i];
	
	for(int i=0;i<N;i++) I[i]=T[i];
	
	for(int i=0;i<N;i++)
	cin>>Result[i];
}

2.插入排序+判断

void InsertSort()
{
	int i;
	
	for(int i=1;i<N;i++)
	{
		int tmp=T[i];
		int j;
		for(j=i;j>0&&T[j-1]>tmp;j--)
			T[j]=T[j-1];
		T[j]=tmp;
		//判断是否与某一步骤吻合 
		int flag=1;
		
		for(int t=0;t<N;t++)
		{
			if(T[t]!=Result[t])
				{
					flag=0;
					break;
				 }	  
		}

		//执行输出 
		if(flag==1){
			cout<<"Insertion Sort"<<endl;
			tmp=T[i+1];
			for(j=i+1;j>0&&T[j-1]>tmp;j--)
				T[j]=T[j-1];
			T[j]=tmp;
			for(int t=0;t<N-1;t++)
			cout<<T[t]<<" ";
			cout<<T[N-1];
			break;
		}
	}
}

3.堆排序
最大堆根结点下沉函数
注意点:
每个child与temp比较,比temp大的放到parent的位置上,若找到比parent小的child,temp放到parent的位置上;
左child的下标为parent2+1,右为2parent+2;

void PercDown(int parent,int n)    //根结点下沉 
{
	int child;
	int temp = I[parent];
	int p;
	for(p=parent;2*p+1<n;p=child)
	{
		child=p*2+1;
		if(child!=n-1&&I[child+1]>I[child])
		++child;

		if(I[child]>temp){     //堆排序关键——与temp比较,并非与parent比较 
			I[p]=I[child];
			
		}
		else break; 
	}
	
	I[p]=temp;
}

将读入的数组转化为最大堆:
从后往前对每一个根节点进行下沉;
注意点
最下方根节点的坐标为n/2-1,因为第一个数下标从0开始;

void BuildHeap(int n)
{
	int i;
	
	for(i=n/2-1;i>=0;i--) 
	{
		PercDown(i,N);
	}
}

堆排序函数+判断+swap函数
每次取堆头与堆尾替换,对堆尾之前的数进行根节点下沉。


void Swap(int &a,int &b)
{
	int temp=a;
	a=b;
	b=temp;
}


void HeapSort()
{
	BuildHeap(N); //变为最大堆
	
	for(int i=N-1;i>=0;i--)
	 {
	 	Swap(I[0],I[i]);
	 	PercDown(0,i);         //等于删除最小堆的一个元素,把堆尾放到头部下沉即可 
	 	
	 	//下沉后判断与result是否相同 
	 	int flag=1;
	 	for(int m=0;m<N;m++)
	 	{
	 		if(I[m]!=Result[m])
	 		{
	 			flag=0;
			 	break;
		 	}
			 }
		 	if(flag==1)
		 	{
		 		cout<<"Heap Sort"<<endl;
		 		Swap(I[0],I[i-1]);
				PercDown(0,i-1); 
				 
				cout<<I[0];
				for(int i=1;i<N;i++)
				cout<<" "<<I[i];
				return;
			 }
	 
}
}

4.main函数

int main()
{
	input();
	BuildHeap(N);
	HeapSort();
	InsertSort();
}

注意点
堆排序较为麻烦;

发布了105 篇原创文章 · 获赞 6 · 访问量 4945

猜你喜欢

转载自blog.csdn.net/BLUEsang/article/details/105555775