zoukankan      html  css  js  c++  java
  • Insertion or Heap Sort

    7-14 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
     1 #include<iostream>
     2 using namespace std;
     3 int main(){
     4     int N; cin>>N;
     5     int i,j,flag=1,tag=0;
     6     int num0[N],num1[N];
     7     for(i=0;i<N;i++)
     8     cin>>num0[i];
     9     for(i=0;i<N;i++)
    10     cin>>num1[i];
    11     for(i=0;i<N-1;i++)
    12     if(num1[i]>num1[i+1]) break;
    13     for(j=i+1;j<N;j++)
    14     if(num1[j]!=num0[j]) flag=0;
    15     if(flag==0){
    16         cout<<"Heap Sort"<<endl;
    17         for(j=1;j<N;j++)
    18         if(num1[(j-1)/2]<num1[j]) break;
    19         j=j-1;
    20         int temp=num1[j];
    21         num1[j]=num1[0];
    22         num1[0]=temp;
    23         j--;
    24         int parent,child;
    25         temp=num1[0];
    26         for(parent=0;2*parent+1<=j;parent=child){
    27             child=parent*2+1;
    28             if(child<j&&num1[child+1]>num1[child])    
    29             child++;
    30             if(num1[child]>temp) num1[parent]=num1[child];
    31             else break;
    32         }
    33         num1[parent]=temp;
    34     }
    35     else{
    36         cout<<"Insertion Sort"<<endl;
    37         int temp=num1[i+1];
    38         for(i=i+1;i>0;i--)
    39         if(num1[i-1]>temp) num1[i]=num1[i-1];
    40         else break;
    41         num1[i]=temp;
    42     }
    43     for(int k=0;k<N;k++)
    44     if(tag++==0) cout<<num1[k];
    45     else cout<<" "<<num1[k];
    46     return 0;
    47 } 
    View Code
     
     
  • 相关阅读:
    js-21点小游戏
    js-打印出现最多次的字母
    盒模型浮动
    九九乘法表
    猫眼-湄公河行动电影介绍页面
    (day4)用css画三角形以及红旗
    cookie的使用
    用Servlet校验密码2
    Servlet登录验证
    Servlet概述
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8056360.html
Copyright © 2011-2022 走看看