本文主要是介绍九、排序(下):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
代码
#include<stdio.h>#define MaxNum 100void Insertion_Next_Iteration(int B[], int i, int N)
{int j, temp = B[i];for( j=i-1; j>=0; --j ){if( B[j]<=temp ){B[j+1] = temp;break;} else B[j+1] = B[j];}if(B[0]>temp)B[0]=temp;for( j=0; j!=N-1; ++j )printf("%d ",B[j]);printf("%d",B[N-1]);
}int Heap_N(int B[], int N)
{/* 得到剩下的最大堆的最后一个结点 */int i, S=0;for( i=N-1; i>=1; --i )if( B[i]<B[i-1] || B[i]<B[0] ){S = 1;break;}if( S==1 )return i;else return 0;
}void RebuildMax(int B[], int n)
{int temp = B[0];int Parent, Child;for( Parent=0; Parent*2+1<=n-1; Parent=Child ){Child = Parent*2+1;if(Child<n-1 && B[Child]<B[Child+1])++Child;if(temp>=B[Child])break;elseB[Parent] = B[Child];}B[Parent] = temp;
}void Heap_Next_Iteration(int A[], int B[], int N)
{int n = Heap_N(B, N);int temp = B[n];B[n] = B[0];B[0] = temp;RebuildMax(B, n);int j;for( j=0; j<N-1; ++j )printf("%d ",B[j]);printf("%d",B[N-1]);
}void Judge(int A[], int B[], int N)
{int i, j, name, S=1;for( i=1; i!=N; ++i )if( B[i]<B[i-1] ) /* 找到有序序列的末尾i-1 */break;for( j=i; j!=N; ++j )if( A[j]!=B[j] ){ /* 看后面的序列有没有进行过操作 */S = 0;break;}if( S ){ /* 后面的序列没动过,说明对前i-1个进行了插入排序 */printf("Insertion Sort\n");Insertion_Next_Iteration(B,i,N);}else{ /* 否则为堆排序 */printf("Heap Sort\n");Heap_Next_Iteration(A,B,N);}}int main()
{int i, N, A[MaxNum], B[MaxNum];scanf("%d\n",&N);for( i=0; i!=N; ++i )scanf("%d",&A[i]);for( i=0; i!=N; ++i )scanf("%d",&B[i]);Judge(A,B,N);return 0;
}
解题思路
和上一道题Insert or Merge基本相同,先判断是不是Insert_Sort,否则就是Heap_Sort。Heaap_Sort再进行一步迭代就是取出最大堆堆顶元素和堆最后元素交换位置,然后再重新调整成最大堆。
这篇关于九、排序(下):Insertion or Heap Sort的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!