PTA 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 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.insertion_sort参考上一篇blog
2.Heapsort是标准三段式
Swap(int A[],int i,int j);//交换函数
void PrecDown(int A[],int start,int N);//自start向下调整
void HeapSort(int A[],int N);//建堆+每次取最大值+调整
注意
1.该题建堆应该单独写一函数,因为在调整前,堆已经建好
所以:
void HeapSort(int A[],int N);//建堆+每次取最大值+调整
拆分成了
//{BuildHeap}
void BuildHeap(int Heap[],int N){
int i;
for(i=N/2-1;i>=0;i--){
PercDown(Heap,i,N);
}
}
//{判断是否堆排序}自下向上
int isHeapSort(int Heap[],int Target[],int N){
for(int i=N-1;i>0;i--){
Swap(Heap,0,i);//DeleteMax
PercDown(Heap,0,i);//调整剩下的元素
if(Match(Heap,Target,N)){
return i;break;
}
}
return -1;
}
2.堆排序和插入排序的方向不同
插入排序是对下一个元素进行sort
sort(Insert,Insert+tempi+1
);
堆排序是对上一个(堆顶方向)进行删除+调整
Swap(Heap,0,Heapi-1
);//DeleteMax
PercDown(Heap,0,Heapi-1
);//调整剩下的元素
if(tempi!=-1){
printf("Insertion Sort\n");
sort(Insert,Insert+tempi+1);
printsort(Insert,N);
}
else if(Heapi!=-1){
printf("Heap Sort\n");
Swap(Heap,0,Heapi-1);//DeleteMax
PercDown(Heap,0,Heapi-1);//调整剩下的元素
printsort(Heap,N);
}
code
#include<iostream>
#include<algorithm>
using namespace std;
//判断两个数组是否完全相同
bool Match(int A[],int B[],int N){
for(int i=0;i<N;i++){
if(A[i]!=B[i]){
return false;
}
}
return true;
}
//打印最终结果
void printsort(int A[],int N){
printf("%d",A[0]);
for(int i=1;i<N;i++){
printf(" %d",A[i]);
}
printf("\n");
}
//返回i否则返回-1
int isInsert(int Insert[],int Target[],int N){
for(int i=2;i<N;i++){
sort(Insert,Insert+i);
if(Match(Insert,Target,N)){
return i;
}
}
return -1;
}
//HeapSort开始
void Swap(int A[],int i,int j){
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
//{从上向下调整的程序}
void PercDown(int Heap[],int start,int N){
int temp=Heap[start];
int parent,child;
for(parent=start;(2*parent+1)<N;parent=child){
child=2*parent+1;
if(child+1<N&&Heap[child+1]>Heap[child])child++;//右孩子存在<N并且比较大
if(temp<Heap[child]){
Heap[parent]=Heap[child];
}else{
break;
}
}
Heap[parent]=temp;
}
//{BuildHeap}
void BuildHeap(int Heap[],int N){
int i;
for(i=N/2-1;i>=0;i--){
PercDown(Heap,i,N);
}
}
//{判断是否堆排序}自下向上
int isHeapSort(int Heap[],int Target[],int N){
for(int i=N-1;i>0;i--){
Swap(Heap,0,i);//DeleteMax
PercDown(Heap,0,i);//调整剩下的元素
if(Match(Heap,Target,N)){
return i;break;
}
}
return -1;
}
int main(){
int N;
scanf("%d",&N);
int Target[N],Insert[N],Heap[N];
for(int i=0;i<N;i++){
scanf("%d",&Insert[i]);//记录初始化状态
Heap[i]=Insert[i];//记录初始化状态
}
for(int i=0;i<N;i++){
scanf("%d",&Target[i]);//记录中间状态
}
BuildHeap(Heap,N);
int tempi=isInsert(Insert,Target,N);
int Heapi=isHeapSort(Heap,Target,N);
if(tempi!=-1){
printf("Insertion Sort\n");
sort(Insert,Insert+tempi+1);
printsort(Insert,N);
}
else if(Heapi!=-1){
printf("Heap Sort\n");
Swap(Heap,0,Heapi-1);//DeleteMax
PercDown(Heap,0,Heapi-1);//调整剩下的元素
printsort(Heap,N);
}
return 0;
}