zoukankan      html  css  js  c++  java
  • PAT A1098 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
    
     
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <set>
     4 #include <string.h>
     5 #include <vector>
     6 #include <queue>
     7 using namespace std;
     8 const int maxn = 100010;
     9 int raw[110],res[110],tmp[110];
    10 bool cmp(int now[],int tar[]){
    11     for(int i=0;i<110;i++){
    12         if(now[i]!=tar[i]) return false;
    13     }
    14     return true;
    15 }
    16 int vis[maxn]={0};
    17 int main(){
    18     int n;
    19     scanf("%d",&n);
    20     for(int i=0;i<n;i++){
    21         scanf("%d",&raw[i]);
    22         tmp[i]=raw[i];
    23     }
    24     for(int i=0;i<n;i++){
    25         scanf("%d",&res[i]);
    26     }
    27     int flag=0,j;
    28     for(j=1;j<=n;j++){
    29         sort(tmp,tmp+j);
    30         if(cmp(tmp,res)) flag=1;
    31         if(flag==1)break;
    32     }
    33     if(flag==1){
    34         printf("Insertion Sort
    ");
    35         sort(raw,raw+j+1);
    36         for(int i=0;i<n;i++){
    37             printf("%d",raw[i]);
    38             if(i!=n-1) printf(" ");
    39         }
    40         return 0;
    41     }
    42 }
    View Code

    注意点:题目完全没说堆排序怎么实现的,对于我这种小白来说,完全不知道堆排序里发生了什么,考试也快到了,先放着吧,只通过插入排序也有11分。未完待续。。

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    webstorm如何调试vue项目的js
    Js调用本地exe的方式
    IE浏览器中使用js调用cmd命令行demo
    JS Array.reverse 将数组元素颠倒顺序
    webstorm 2018.2.5最新激活方式
    Vmware12安装centos系统详解
    如何查看电脑系统版本信息和显示文件后缀名
    怎样用命令行开启或关闭Windows服务
    Vue编写的todolist小例子
    Word如何发布文章到博客园开源中国网易博客
  • 原文地址:https://www.cnblogs.com/tccbj/p/10447383.html
Copyright © 2011-2022 走看看