Insert Sort + Heap Sort 09- Sort 3 Insertion or Heap Sort (25 points)

09-Sort 3 Insertion or Heap Sort (25 points)

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

Problem-solving,
write heap sorting and split-in sorting, each step is judged;

1. Input function
For convenience, T is used for insertion sort judgment, and I is used for heap sort judgment;

#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. Insert sort + judge

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. Heap sorting
Maximum heap root node sinking function
Note :
Each child is compared with temp, and it is larger than temp and placed in the position of parent. If a child smaller than parent is found, temp is placed in the position of parent;
left The subscript of child is parent 2 + 1, and the right is 2 parent + 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;
}

Convert the read array into the maximum heap:
sink each root node from back to front ;
note :
the coordinate of the bottom root node is n / 2-1, because the first number index starts from 0;

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

Heap sort function + judgment + swap function
Each time the head and end of the heap are replaced, the root node sinks to the number before the end of the heap.


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 function

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

Note that
heap sorting is more troublesome;

Published 105 original articles · won praise 6 · views 4945

Guess you like

Origin blog.csdn.net/BLUEsang/article/details/105555775