• 09-排序3 Insertion or Heap Sort(浙大数据结构)


    09-排序3 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:

    1. 10
    2. 3 1 2 8 7 5 9 4 6 0
    3. 1 2 3 7 8 5 9 4 6 0

    Sample Output 1:

    1. Insertion Sort
    2. 1 2 3 5 7 8 9 4 6 0

    Sample Input 2:

    1. 10
    2. 3 1 2 8 7 5 9 4 6 0
    3. 6 4 5 1 0 3 2 7 8 9

    Sample Output 2:

    1. Heap Sort
    2. 5 4 3 1 0 2 6 7 8 9

    1. #include <iostream>
    2. #include <algorithm>
    3. using namespace std;
    4. const int maxn = 110;
    5. int heap[maxn],a[maxn],res[maxn];
    6. int n;
    7. bool InsertSort(int *A,int num)
    8. {
    9. int flag=0;
    10. for(int i=2;i<=num;++i)
    11. {
    12. int temp=A[i],j;
    13. for(j=i;j>1 && A[j-1]>temp;--j)
    14. A[j]=A[j-1];
    15. A[j]=temp;
    16. if(flag==1)
    17. return true;
    18. for(int k=1;k<=num;++k)
    19. {
    20. if(A[k]==res[k])
    21. flag=1;
    22. else
    23. {
    24. flag=0;
    25. break;
    26. }
    27. }
    28. }
    29. return false;
    30. }
    31. void downAdjust(int low,int high)
    32. {
    33. int par=low,chi=2*par;
    34. while(chi<=high)
    35. {
    36. if(chi!=high && heap[chi+1]>heap[chi])
    37. ++chi;
    38. if(heap[chi]>heap[par])
    39. {
    40. swap(heap[chi],heap[par]);
    41. par=chi;
    42. chi*=2;
    43. }
    44. else
    45. break;
    46. }
    47. }
    48. void CreateHeap(int num)
    49. {
    50. for(int i=num/2;i>0;--i)
    51. downAdjust(i,num);
    52. }
    53. void HeapSort(int num)
    54. {
    55. CreateHeap(num);
    56. int flag=0;
    57. for(int i=num;i>0;--i)
    58. {
    59. swap(heap[1],heap[i]);
    60. downAdjust(1,i-1);
    61. if(flag==1)
    62. return;
    63. for(int j=1;j<=num;++j)
    64. {
    65. if(heap[j]==res[j])
    66. flag=1;
    67. else
    68. {
    69. flag=0;
    70. break;
    71. }
    72. }
    73. }
    74. }
    75. int main()
    76. {
    77. cin >> n;
    78. for(int i=1;i<=n;++i)
    79. {
    80. cin >> heap[i];
    81. a[i]=heap[i];
    82. }
    83. for(int i=1;i<=n;++i)
    84. cin >> res[i];
    85. if(InsertSort(a,n)==true)
    86. {
    87. puts("Insertion Sort");
    88. for(int i=1;i<=n;++i)
    89. {
    90. if(i==1)
    91. cout << a[i];
    92. else
    93. cout << ' ' << a[i];
    94. }
    95. cout << endl;
    96. }
    97. else
    98. {
    99. HeapSort(n);
    100. puts("Heap Sort");
    101. for(int i=1;i<=n;++i)
    102. {
    103. if(i==1)
    104. cout << heap[i];
    105. else
    106. cout << ' ' << heap[i];
    107. }
    108. cout << endl;
    109. }
    110. return 0;
    111. }

  • 相关阅读:
    常用 的 Vue3 新增方法及用法
    手把手教你写一个图片预览组件
    java 面向对象
    附录A 程序员工作面试的秘密
    数据结构(一)C语言版:绪论——数据结构基本概念真题
    Unity 设置窗口置顶超级详解版
    Pytorch tensor 数据类型快速转换三种方法
    密码学算法教程
    安装konga
    渗透测试学习day3
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/126676189