zoukankan      html  css  js  c++  java
  • 1098 Insertion or Heap Sort (25 分)

    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 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
    思路
      题目中给出的第二个序列是直接插入排序后者是堆排序的局部结果。如果是直接插入排序,则第二个序列的前半段必定有序满足b[1]<b[2],
    如果是堆排序,则必定有b[1]>b[2]因为是大顶堆。
    #include<iostream>
    #include<string>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    
    
    void adjustDown(int a[],int low,int high)
    {
        int i=low;
        int j=2*i;
        while(j<=high)
        {
            if(j+1<=high&&a[j]<a[j+1])
            {
                j++;
            }
            if(a[j]>a[i])
            {
                swap(a[j],a[i]);
                i=j;
                j=i*2;
            }
            else
            {
                break;
            }
        }
    }
    
    
    
    void buildHeap(int a[],int n)
    {
        for(int i=n/2;i>=1;i--)
            adjustDown(a,i,n);
    }
    
    
    
    int main()
    {
        int n;
        scanf("%d",&n);
        int a[n+1],b[n+1],maxV=-10000000;
        for(int i=1; i<n+1; i++)
        {
            scanf("%d",&a[i]);
            maxV=max(maxV,a[i]);
        }
        for(int i=1; i<n+1; i++)
            scanf("%d",&b[i]);
        b[0]=-100;
        if(b[1]<b[2])
        {
            printf("Insertion Sort
    ");
            int index=2;
            while(b[index-1]<=b[index]) index++;
            int temp=b[index];
            index--;
            while(temp<b[index])
            {
                b[index+1]=b[index];
                index--;
            }
            b[index+1]=temp;
            printf("%d",b[1]);
            for(int i=2; i<n+1; i++)
                printf(" %d",b[i]);
        }
        else
        {
            printf("Heap Sort
    ");
            int temp=b[1];
            int index=n;
    
            while(b[index]>temp)index--;
            swap(b[1],b[index]);
            adjustDown(b,1,index-1);
            printf("%d",b[1]);
            for(int i=2; i<n+1; i++)
                printf(" %d",b[i]);
        }
        return 0;
    }
     
  • 相关阅读:
    MFC记录
    【转】linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解----不错
    【转】五大绝招复制不能复制的网页文字
    【转】DELL戴尔N4050笔记本拆机(图文)
    【转】Linux下tar.xz结尾的文件的解压方法
    【转】在Ubuntu 12.04 上为Virtualbox 启用USB 设备支持--不错
    【转】Android adb shell操作时出现“ XXX ... Read-only file system”解决办法--不错
    【转】VirtualBox direct access to SD Card in Windows--不错
    网址
    TensorFlow 学习(九)—— 初始化函数(概率分布函数 api、常数生成函数)
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10369319.html
Copyright © 2011-2022 走看看