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

题意:

给出一个序列的初始状态以及在排序过程中的一种状态,根据这两种状态来判断属于那种排序。

思路:

一开始没什么思路,因为之前看过浙大关于数据结构的MOOC的课程,记得里面有一课是讲关于插入排序和归并排序的,于是就又看了一遍。里面讲的一种算法挺有意思的叫做“捏软柿子”算法,简单的来讲就是那种好判断就判断那种,如果不属于这种那么就属于另外一种,看过之后有一种豁然开朗的感觉,感叹在生后中真的是算法无处不在。

之前对堆排序也只是一知半解,只是把书本上的东西看了一遍,并没有多深的理解,有种纸上谈兵的感觉。今天通过这道题有了一个更深的理解。里面有一个重要的函数来实现大顶堆,代码如下:

void maxHeapify(vector<int>& a, int start, int end) {
    int dad = start;
    int son = dad * 2 + 1;
    while (son <= end) {
        if (son + 1 <= end && a[son] < a[son+1]) 
            son += 1;
        if (a[dad] > a[son]) return ;
        else {
            swap(a[dad], a[son]);
            dad = son;
            son = dad * 2 + 1;
        }
    }

}

Code:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int n;
vector<int> bef, pro, aft;

void maxHeapify(vector<int>& a, int start, int end) {
    int dad = start;
    int son = dad * 2 + 1;
    while (son <= end) {
        if (son + 1 <= end && a[son] < a[son+1]) 
            son += 1;
        if (a[dad] > a[son]) return ;
        else {
            swap(a[dad], a[son]);
            dad = son;
            son = dad * 2 + 1;
        }
    }

}

int findLast(vector<int>& a, vector<int>& b) {
    int i;
    for (i = n-1; i >= 0; --i) {
        if (a[i] == b[i]) continue;
        else break;
    }
    return i;
}

void swap(int &a, int &b) {
    int temp = a;
    a = b; 
    b = temp;
}

bool isInsertSort(vector<int>& a, vector<int>& b) {
    int i;
    for (i = 1; i < n; ++i) {
        if (b[i-1] < b[i]) continue;
        else break;
    }
    for (; i < n; ++i) {
        if (b[i] == a[i]) continue;
        else break;
    }
    if (i == n) return true;
    else return false;
}

int main() {
    cin >> n;

    int temp;
    for (int i = 0; i < n; ++i) {
        cin >> temp;
        bef.push_back(temp);
        aft.push_back(temp);
    }
    sort(aft.begin(), aft.end());
    for (int i = 0; i < n; ++i) {
        cin >> temp;
        pro.push_back(temp);
    }

    if (isInsertSort(bef, pro)) {
        for (int i = 1; i < n; ++i) {
            if (pro[i-1] < pro[i]) continue;
            else {
                for (int j = i; j >= 0; --j) {
                    if (pro[j-1] > pro[j]) swap(pro[j-1], pro[j]);
                    else break;
                }
                break;
            }
        }
        cout << "Insertion Sort" << endl;
    } else {
        int last = findLast(pro, aft);
        swap(pro[0], pro[last--]);      
        for (int i = last/2; i >= 0; --i) {
            maxHeapify(pro, i, last);
        }
        cout << "Heap Sort" << endl;
    }

    cout << pro[0];
    for (int i = 1; i < n; ++i) 
        cout << " " << pro[i];

    cout << endl;

    return 0;
}

  

最后有一组数据没有通过,再一次感到拿满分真的很不容易。


看了一下别人的代码,发现自己写的代码有很多都很鸡肋,把代码优化了一下,但是那组数据还是没有通过。

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int n;
vector<int> bef, pro;

void maxHeapify(vector<int>& a, int start, int end) {
    int dad = start;
    int son = dad * 2 + 1;
    while (son <= end) {
        if (son + 1 <= end && a[son] < a[son+1]) 
            son += 1;
        if (a[dad] > a[son]) return ;
        else {
            swap(a[dad], a[son]);
            dad = son;
            son = dad * 2 + 1;
        }
    }
}

void swap(int &a, int &b) {
    int temp = a;
    a = b; 
    b = temp;
}

int main() {
    cin >> n;

    int temp;
    for (int i = 0; i < n; ++i) {
        cin >> temp;
        bef.push_back(temp);
    }
    for (int i = 0; i < n; ++i) {
        cin >> temp;
        pro.push_back(temp);
    }

    int p1 = 1, p2;
    while (p1 < n && pro[p1-1] < pro[p1]) p1++;
    p2 = p1;
    while (p2 < n && pro[p2] == bef[p2]) p2++;

    if (p2 == n) {
        sort(pro.begin(), pro.begin()+p1+1);
        cout << "Insertion Sort" << endl;
    } else {
        int last = n - 1;
        while (last > 0 && pro[last] >= pro[0]) last--;
        swap(pro[0], pro[last--]);      
        for (int i = last/2; i >= 0; --i) {
            maxHeapify(pro, i, last);
        }
        cout << "Heap Sort" << endl;
    }

    cout << pro[0];
    for (int i = 1; i < n; ++i) 
        cout << " " << pro[i];

    cout << endl;

    return 0;
}

  

参考:

https://www.liuchuo.net/archives/2273

永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/12606090.html