题目描述
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?
输入描述:
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.
输出描述:
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.
输入例子:
10 3 1 2 8 7 5 9 4 6 0 1 2 3 7 8 5 9 4 6 0
输出例子:
Insertion Sort 1 2 3 5 7 8 9 4 6 0
该题题意简明,我用的方法相对暴力,直接模拟两种排序进行一一比对。
堆排序时,大根堆的建立是自底向上,之后的调整是自顶向下,明白了这一点调整函数就比较好写了
1 #include<bits/stdc++.h> 2 using namespace std; 3 int initial[105],sorted[105]; 4 void adjust(int *arr,int head,int tail){ //自head往tail调整 5 int temporal_max; //子树中更大的一棵 6 while(head*2<=tail){ 7 if (head*2<tail){//该父节点有两棵子树 8 temporal_max=arr[head*2]>arr[head*2+1]?head*2:head*2+1; 9 } 10 else{ //该父节点只有一棵子树 11 temporal_max=head*2; 12 } 13 if (arr[temporal_max]>arr[head]){ 14 int temp=arr[temporal_max]; 15 arr[temporal_max]=arr[head]; 16 arr[head]=temp; 17 } 18 else{ 19 break; 20 } 21 head=temporal_max; 22 } 23 } 24 25 int main(){ 26 int n; 27 cin>>n; 28 memset(initial,0,sizeof(initial)); 29 memset(sorted,0,sizeof(sorted)); 30 for(int i=1;i<=n;++i){ 31 cin>>initial[i];//堆排序为了方便计算下标从1开始 32 } 33 for(int i=1;i<=n;++i){ 34 cin>>sorted[i]; 35 } 36 //判断是否为插入排序 37 int flag; 38 for(int i=1;i<=n;++i){ 39 sort(initial+1,initial+i+1); 40 flag=1; 41 for (int j=1;j<=n;++j){ 42 if (initial[j]!=sorted[j]){ 43 flag=0; 44 break; 45 } 46 } 47 if (flag==1){ //判定为插入排序 48 cout<<"Insertion Sort"<<endl; 49 sort(initial+1,initial+i+2); 50 for (int k=1;k<=n;++k){ 51 cout<<initial[k]; 52 if (k!=n) 53 cout<<" "; 54 } 55 break; 56 } 57 } 58 if (flag==0){ //判定为堆排序 59 cout<<"Heap Sort"<<endl; 60 sort(initial+1,initial+n+1); 61 int index; 62 for (int i=n;i>0;--i){ 63 if (initial[i]!=sorted[i]){ 64 index=i; //定位到当前趟数 65 break; 66 } 67 } 68 //下一趟堆排序 69 int temp=sorted[1]; 70 sorted[1]=sorted[index]; 71 sorted[index]=temp; 72 adjust(sorted,1,index-1); 73 for (int i=1;i<=n;++i){ 74 cout<<sorted[i]; 75 if (i!=n){ 76 cout<<" "; 77 } 78 } 79 } 80 }
标签:Sort,25,head,int,temporal,initial,arr,Heap,sorted From: https://www.cnblogs.com/coderhrz/p/16706433.html