zoukankan      html  css  js  c++  java
  • 1098. 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 (<=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
    


    题目要求对给定的排序未完成序列判断是插入排序还是堆排序的中间步,并继续排序到下一步输出,实质就是考察两种排序的写法。

    对于插入排序和堆排序,算法导论上给出了简洁易懂的代码,这里不再赘述。

    这两种排序的每一步都十分明确,不像归并排序那样难以处理,因此我们在每一步都进行判断即可,一旦发现序列全等,就把一个标志位置为true,在下一轮排序中检测到true直接输出即可,这里可以用到vector的=重载,方便的判断两个序列的全等性。

    需要注意的是,为了方便堆排序,我们的序列从1~N为有效部分,0放置哨兵INF方便堆的调整,因此各个序列初始化时都需要向索引0位置写INF

    代码如下:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <stdio.h>
    
    using namespace std;
    
    #define INF 99999999
    
    struct Heap{
        int size;
        vector<int> data;
    
        Heap(int n){
            size = n;
            data.resize(n + 1);
            data[0] = INF;
        }
    
    public:
        void printData(){
            printf("%d",data[1]);
            for(int i = 2; i < data.size(); i++){
                printf(" %d",data[i]);
            }
            cout << endl;
        }
    
    };
    
    void max_heapify(Heap &hp, int i){
        int left = i * 2;
        int right = i * 2 + 1;
        int largest = i;
        if(left <= hp.size && hp.data[left] > hp.data[i]) largest = left;
        if(right <= hp.size && hp.data[right] > hp.data[largest]) largest = right;
        if(largest != i){
            int temp = hp.data[largest];
            hp.data[largest] = hp.data[i];
            hp.data[i] = temp;
            max_heapify(hp,largest);
        }
    }
    
    void buildHeap(Heap &hp){
        hp.size = hp.data.size() - 1;
        for(int i = hp.size / 2; i >= 1; i--)
            max_heapify(hp,i);
    
    }
    
    int main()
    {
        int N;
        cin >> N;
        vector<int> tempData(N + 1);
        vector<int> sortedData(N + 1);
        sortedData[0] = INF;
        tempData[0] = INF;
        Heap hp = Heap(N);
        for(int i = 1; i <= N; i++){
            scanf("%d",&hp.data[i]);
            tempData[i] = hp.data[i];
        }
        for(int i = 1; i <= N; i++){
            scanf("%d",&sortedData[i]);
        }
        bool isHeapSort = false;
        buildHeap(hp);
        for(int i = N; i >= 2; i--){
            int temp = hp.data[1];
            hp.data[1] = hp.data[i];
            hp.data[i] = temp;
            hp.size--;
            max_heapify(hp,1);
            if(isHeapSort){
                printf("Heap Sort
    ");
                hp.printData();
                return 0;
            }
            if(hp.data == sortedData){
                isHeapSort = true;
            }
        }
        bool endFlag = false;
        for(int j = 2; j <= N; j++){
            int key = tempData[j];
            int i = j - 1;
            while(i > 0 && tempData[i] > key){
                tempData[i + 1] = tempData[i];
                i--;
            }
            tempData[i + 1] = key;
            if(endFlag){
                printf("Insertion Sort
    %d",tempData[1]);
                for(int i = 2; i <= N; i++)
                    printf(" %d",tempData[i]);
                cout << endl;
                return 0;
            }
            if(tempData == sortedData){
                endFlag = true;
            }
        }
    
        return 0;
    }
    


  • 相关阅读:
    composer阿里云短信服务不支持传参为数值--为2017年短信接口,2018阿里云有更新http://www.cnblogs.com/q1104460935/p/8916096.html
    随机生成字符串,数字,手机号,邮箱
    C#: .net序列化及反序列化 [XmlElement(“节点名称”)] [XmlAttribute(“节点属性”)] (上篇)
    自动升级功能
    C# WinForm 设置按纽为透明,使用背景色
    sql server 2000 单主键高效分页存储过程 (支持多字段排序)
    分页存储过程
    C# WinForm 解决子窗体放大后,子窗体图标放大的问题
    Windows 7/8 64位系统 不能注册32位dll 文件的解决方案
    添加ico图标
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154045.html
Copyright © 2011-2022 走看看