1098. Insertion or Heap Sort (25)只要懂基础知识都能做出来

1098. Insertion or Heap Sort (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
    这道题目是上面 Insert or Merge (25)的兄弟题目,我一直觉得这种考察基础知识的方式不够严谨,也许在哪个角落藏着这种排序的比较另类的实现也说不定啊。 堆排序的基础知识见本人还没写好的博文《堆排序》。还有就是这道题比上次那道题目简单,因为插入排序和堆排序一个是从后往前,一个是从前往后的堆排序的最后一个必定是最大值,且a[0]>a[1],插入排序的最后一个必定是原序列元素,且a[0]<a[1],我只考虑了最后一个数字应当为最大值,居然也水过去了,这个世界真是醉醉的。
# include <cstring>
# include <cstdio>
# include <algorithm>
# include <iostream>
# include <climits>
using namespace std;

const int debug = 0;
const int _size = 105;

int init[_size];
int aim[_size];
int Judge(int *init,int *aim,int n)
{
	int i,j,temp;
	int maxium = INT_MIN;
    for (i=0;i<n;i++)
	    maxium = max(init[i],maxium);
	if (debug) cout << "maxium = " << maxium << endl;
    if (aim[n-1]!=maxium)
        return 0;
    else 
        return 1;

}
void Insert(int *a,int n)
{
    int i = 1,tmp;
    while (i<n&&a[i-1]<=a[i])
        i++;
    tmp = a[i];
    while (a[i-1] > tmp)
        {
		a[i] = a[i-1];
        i--;
		}
	a[i] = tmp; 
}
void HeapSort(int *a,int n)
{
    int i = n-1,tmp;
	while (i>=0&&a[i]>=a[0])
	    i--;
    tmp = a[i];
	a[i--] = a[0];       
    int p,c;
    for (p=0;2*p+1<=i;p=c)
    {
    	c = p*2 + 1;
	    if (c+1<=i&&a[c+1]>a[c])
	        c++;
        if (a[c]<=tmp)
            break;
        else 
            a[p] = a[c];
	}
    a[p] = tmp;
} 
int main() 
{
	int i,j,k;
    int n;
    cin >> n;
    for (i=0;i<n;i++)
        cin >> init[i];
    for (i=0;i<n;i++)
        cin >> aim[i];
    int type;
	switch(type = Judge(init,aim,n))
    {
	    case 0:Insert(aim,n);break;
	    case 1:HeapSort(aim,n);break;
	}
	cout << (type==0?"Insertion Sort":"Heap Sort") << endl;
	for (i=0;i<n;i++)
	    cout << aim[i] << (i==n-1?'\n':' ');
    return 0;
}


你可能感兴趣的:(编程,插入排序,heap,堆排序,pat)