PTA 09-排序3 Insertion or Heap Sort(25 分)

题目:

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 (). 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


胡乱分析:

题目给你一个原数组,和一个经过排序之后的数组,但是只排到一半,没有排完,让你判断是堆排序还是插入排序,并且输出下一步排序之后的数组 。判断还是挺容易的,由于在插入排序中数组前面的部分是排序完的,即严格递增(减)的,而_剩下部分和原数组时相同的,根据这点就能判断。而堆排序中数组已拍序完的部分时严格递增(减)的,然后找到index偷懒用一下STL的pop_heap(b,b + index + 1)就搞定啦!得益于c++强大的STL的sort和heap的相关操作,整个程序的代码十分简洁!(才不是为自己偷懒找借口呢!!

代码:

#include 
#include 
using namespace std;
int main() {
    int N;
    cin >> N;
    int *a = new int[N];
    int *b = new int[N];
    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < N; i++) {
        cin >> b[i];
    }

    int index;
    for (index = 1; b[index] >= b[index - 1]; index++);
    int count = index;//已经排好序的元素的个数
    while (count < N && a[count] == b[count]) {
        count++;//没有排序的个数
    }

    if (count == N) {//如果count和元素个数相等的话   插排
        cout << "Insertion Sort\n";
        sort(b, b + index + 1);
        cout << b[0];
        for (int i = 1; i < N; i++) {
            cout << " " << b[i];
        }
    }
    else {//堆排序
        cout << "Heap Sort\n";
        sort(a, a + N);//把A排序
        for (index = N - 1; index > 0 && b[index] == a[index]; index--);//从末尾数相同元素的个数
        //for (index = N - 1; index > 0 && b[index] > b[index - 1]; index--);
        pop_heap(b, b + index + 1);
        cout << b[0];
        for (int i = 1; i < N; i++) {
            cout << " " << b[i];
        }
    }
}

//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
//
//10
//3 1 2 8 7 5 9 4 6 0
//6 4 5 1 0 3 2 7 8 9
//Heap Sort
//5 4 3 1 0 2 6 7 8 9

你可能感兴趣的:(数据结构,PAT数据结构)