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

    堆排序:https://www.cnblogs.com/chengxiao/p/6129630.html
    分析:https://blog.csdn.net/liuchuo/article/details/52252172
    代码:
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 void adjust(int v[], int s, int e)
     5 {
     6     int cur = 1;
     7     while (1)
     8     {
     9         int l = cur << 1, r = cur << 1 | 1;
    10         if (l <= e && r <= e)
    11         {
    12             if (v[l] < v[r])
    13                 swap(v[cur], v[r]), cur = r;
    14             else swap(v[cur], v[l]), cur = l;
    15         }
    16         else if (l <= e && r > e)
    17             swap(v[cur], v[l]), cur = l;
    18         else break;
    19         
    20     }
    21 }
    22 
    23 int main()
    24 {
    25     int n;
    26     int a[110], b[110];
    27     cin >> n;
    28     for (int i = 1; i <= n; i++)
    29         cin >> a[i];
    30     for (int i = 1; i <= n; i++)
    31         cin >> b[i];
    32     int pos = 1;
    33     for (; pos + 1 <= n && b[pos] <= b[pos + 1]; pos++)
    34         ;
    35     int flag = 0;
    36     for(int i = pos + 1; i <= n; i++)
    37         if (a[i] != b[i])
    38         {
    39             flag = 1;
    40             break;
    41         }
    42     if (!flag)
    43     {
    44         cout << "Insertion Sort" << endl;
    45         sort(b + 1, b + 1 + pos + 1);
    46         for (int i = 1; i <= n; i++)
    47             printf("%d%c", b[i], i == n ? '
    ' : ' ');
    48     }
    49     else
    50     {
    51         cout << "Heap Sort" << endl;
    52         int pos = n;
    53         for (; pos >= 1 && b[pos] >= b[1]; pos--);
    54         swap(b[1], b[pos]); pos--;
    55         adjust(b, 1, pos);
    56         for (int i = 1; i <= n; i++)
    57             printf("%d%c", b[i], i == n ? '
    ' : ' ');
    58     }
    59 }
  • 相关阅读:
    结对(求数组的最大子数组和)
    结对3(求一维数组最大子数组的和扩展)
    结对3(电梯调度需求分析)
    结对开发2(求二维数组的最大子数组和)
    四则运算3(四则运算2程序的扩展)
    四则运算2代码测试
    四则运算2程序及运行
    C++输出四则运算设计题的思路
    C++编程显示四则运算题目
    软件课外读物阅读计划
  • 原文地址:https://www.cnblogs.com/liuwenhan/p/11823076.html
Copyright © 2011-2022 走看看