PAT 1098-Insertion or Heap Sort (25)

来源:互联网 发布:输入框获取焦点js 编辑:程序博客网 时间:2024/06/05 03:30

题目描述

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.

输入例子:

103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0

输出例子:

Insertion Sort1 2 3 5 7 8 9 4 6 0


解题思路

一道关于排序算法的题目,比较暴力的方法是直接运行两种排序算法,每迭代一次就比较一次,若和输入的中间排序队列相同则跳出,从而得出所用的排序类型。

另一种方法是根据题目所说的排序结果唯一,要么是堆排序要么是插入排序。所以我们只要判断排序是否是插入排序即可,因为插入排序的中间队列的后面几个数字和原始队列的数字是相同的,二前面的数字则是排序好的。根据这个特性我们很容易推断出排序是否是插入排序。


#include<iostream>#include<vector>#include <algorithm>using namespace std;#define LeftChild(i) (2*(i)+1)void HeapSort(vector<int>& A, int i, int N);int main(){int N; cin >> N;vector<int> startseq(N);vector<int> midseq(N);int test[5];for (int i = 0; i < N; i++)cin >> startseq[i];for (int i = 0; i < N; i++)cin >> midseq[i];bool isinsert = true;int temp = N-1;while (temp >= 0 && midseq[temp] == startseq[temp])temp--;for(int i = temp; i >0; i--)if (midseq[i] < midseq[i - 1]){isinsert = false;break;}if (isinsert){cout << "Insertion Sort" << endl;sort(midseq.begin(), midseq.begin() + temp+2);}else{cout << "Heap Sort" << endl;temp = N-1;while (temp >= 0 && midseq[temp] >= midseq[0]) {temp--;}swap(midseq[temp], midseq[0]);HeapSort(midseq, 0, temp);}for (int i = 0; i < N-1; i++)cout << midseq[i] << " ";cout << midseq[N - 1];return 0;}void HeapSort(vector<int>& A, int i, int N){int Child, temp;for (temp = A[i]; LeftChild(i) < N; i = Child){Child = LeftChild(i);if (Child != N - 1 && A[Child + 1] > A[Child])Child++;if (temp < A[Child])A[i] = A[Child];elsebreak;}A[i] = temp;}


0 0
原创粉丝点击