1. 程式人生 > >《資料結構》09-排序3 Insertion or Heap Sort

《資料結構》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 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 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<iostream>
#include<algorithm>
using namespace std;
void output(int A[],int N); 

// 判斷是否相等 
bool judge(int A[],int B[],int N){
	for(int i=0;i<N;i++)
		if(A[i]!=B[i])
			return false;
	return true;
} 

// 插入排序 
bool Insertion_Sort(int A[],int B[],int N){
	for(int p=1;p<N;p++){
		int tmp = A[p];
		int k = p;
		for(;k>0 && tmp < A[k-1];k--)
			A[k] = A[k-1];
		A[k] = tmp;
		if(judge(A,B,N)){// 如果相等,多做一輪
			p++; 
			int tmp = A[p];
			int k = p;
			for(;k>0 && tmp < A[k-1];k--)
				A[k] = A[k-1];
			A[k] = tmp;
			return false;
		}
	}
	return true;
}

void PrecDown(int A[],int i,int N){
	int tmp = A[i]; // 取得當前"根"
	int parent,child;
	for(parent=i;parent*2+1<N;parent = child){
		child = 2*parent +1;
		if((child!=N-1) && (A[child] < A[child+1]))
			child++;
		if(A[child] <= tmp)
			break;
		A[parent] = A[child];
	} 
	A[parent] = tmp;
}

// 堆排序 
bool Heap_Sort(int A[],int B[],int N){
	for(int i=N/2;i>=0;i--)
		PrecDown(A,i,N);
	for(int i=N-1;i>0;i--){
		swap(A[0],A[i]);
		PrecDown(A,0,i);
		if(judge(A,B,N)){  // 如果相等,多做一輪 
			swap(A[0],A[--i]);
			PrecDown(A,0,i);
			return false;
		}
	}
	return true;
}

// 輸出 
void output(int A[],int N){
	for(int i=0;i<N;i++){
		if(i)
			cout<<" ";
		cout<<A[i];
	}
}

int main(){
	int N;
	cin>>N;
	int A[N],tmpA[N];
	for(int i=0;i<N;i++){
		cin>>A[i];
		tmpA[i] = A[i];
	}
	int B[N];
	for(int i=0;i<N;i++) 
		cin>>B[i];
	if(!Heap_Sort(tmpA,B,N)){  // 如果是堆排序 
		cout<<"Heap Sort"<<endl;
		output(tmpA,N);
		return 0;
	}
	if(!Insertion_Sort(A,B,N)){  // 如果是插入排序 
		cout<<"Insertion Sort"<<endl;
		output(A,N);
		return 0;
	}
	return 0;
}