fork download
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. bool is_power_of_two(int i)
  6. {
  7. return (i & (i - 1)) == 0;
  8. }
  9.  
  10. void heap_print(int a[], int n)
  11. {
  12. for (int i = 0; i < n; ++i)
  13. {
  14. if (is_power_of_two(i + 1))
  15. {
  16. printf("| ");
  17. }
  18. printf("%2d ", a[i]);
  19. }
  20. putchar('\n');
  21. }
  22.  
  23. int father_of(int i)
  24. {
  25. return (i - 1) >> 1;
  26. }
  27.  
  28. bool is_heap(int a[], int n)
  29. {
  30. for (int i = 1; i < n; ++i)
  31. {
  32. if (a[father_of(i)] < a[i]) return false;
  33. }
  34. return true;
  35. }
  36.  
  37. void heap_insert(int a[], int i)
  38. {
  39. int element = a[i];
  40. int father;
  41. while ((0 < i) && a[father = father_of(i)] < element)
  42. {
  43. a[i] = a[father];
  44. i = father;
  45. }
  46. a[i] = element;
  47. assert(is_heap(a, i+1));
  48. }
  49.  
  50. int max_child(int a[], int n, int i)
  51. {
  52. int left_child = (i << 1) + 1;
  53. if (left_child >= n) return n;
  54.  
  55. int right_child = left_child + 1;
  56. /*
  57. In the rare case where i has only one child, right_child == n.
  58. Since a[n] == element, the remove loop would terminate correctly,
  59. so it does not have to be handled explicitly as a special case.
  60. */
  61. return a[left_child] < a[right_child] ? right_child : left_child;
  62. }
  63.  
  64. void heap_remove(int a[], int n)
  65. {
  66. int maximum = a[0];
  67. int element = a[n];
  68. int i = 0;
  69. int child;
  70. while (element < a[child = max_child(a, n, i)])
  71. {
  72. a[i] = a[child];
  73. i = child;
  74. }
  75. a[i] = element;
  76. a[n] = maximum;
  77. assert(is_heap(a, n));
  78. }
  79.  
  80. void heapsort(int a[], int n)
  81. {
  82. for (int i = 1; i < n; ++i)
  83. {
  84. heap_print(a, i);
  85. heap_insert(a, i);
  86. }
  87. heap_print(a, n);
  88. for (int i = n - 1; i > 0; --i)
  89. {
  90. heap_remove(a, i);
  91. heap_print(a, i);
  92. }
  93. }
  94.  
  95. void randomize(int a[], int n)
  96. {
  97. for (int i = 0; i < n; ++i)
  98. {
  99. a[i] = 10 + rand() % 90;
  100. }
  101. }
  102.  
  103. bool is_sorted(int a[], int n)
  104. {
  105. for (int i = 1; i < n; ++i)
  106. {
  107. if (a[i-1] > a[i]) return false;
  108. }
  109. return true;
  110. }
  111.  
  112. int sum(int a[], int n)
  113. {
  114. int s = 0;
  115. for (int i = 0; i < n; ++i)
  116. {
  117. s += a[i];
  118. }
  119. return s;
  120. }
  121.  
  122. enum { N = 23 };
  123.  
  124. int main()
  125. {
  126. int a[N];
  127. randomize(a, N);
  128.  
  129. int old_sum = sum(a, N);
  130. heapsort(a, N);
  131. int new_sum = sum(a, N);
  132.  
  133. assert(old_sum == new_sum);
  134. assert(is_sorted(a, N));
  135. }
  136.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
| 83 
| 83 | 26 
| 83 | 26 37 
| 83 | 35 37 | 26 
| 83 | 35 37 | 26 33 
| 83 | 35 37 | 26 33 35 
| 83 | 35 56 | 26 33 35 37 
| 83 | 35 56 | 26 33 35 37 | 22 
| 83 | 79 56 | 35 33 35 37 | 22 26 
| 83 | 79 56 | 35 33 35 37 | 22 26 11 
| 83 | 79 56 | 35 42 35 37 | 22 26 11 33 
| 83 | 79 77 | 35 42 56 37 | 22 26 11 33 35 
| 83 | 79 77 | 35 42 60 37 | 22 26 11 33 35 56 
| 89 | 79 83 | 35 42 60 77 | 22 26 11 33 35 56 37 
| 89 | 79 83 | 35 42 60 77 | 22 26 11 33 35 56 37 33 
| 89 | 86 83 | 79 42 60 77 | 35 26 11 33 35 56 37 33 | 22 
| 89 | 86 83 | 79 42 60 77 | 70 26 11 33 35 56 37 33 | 22 35 
| 89 | 86 83 | 79 42 60 77 | 70 46 11 33 35 56 37 33 | 22 35 26 
| 89 | 86 83 | 79 42 60 77 | 70 62 11 33 35 56 37 33 | 22 35 26 46 
| 89 | 86 83 | 79 56 60 77 | 70 62 42 33 35 56 37 33 | 22 35 26 46 11 
| 89 | 86 83 | 79 81 60 77 | 70 62 56 33 35 56 37 33 | 22 35 26 46 11 42 
| 89 | 86 83 | 79 81 60 77 | 70 62 56 33 35 56 37 33 | 22 35 26 46 11 42 18 
| 97 | 89 83 | 79 86 60 77 | 70 62 56 81 35 56 37 33 | 22 35 26 46 11 42 18 33 
| 89 | 86 83 | 79 81 60 77 | 70 62 56 33 35 56 37 33 | 22 35 26 46 11 42 18 
| 86 | 81 83 | 79 56 60 77 | 70 62 42 33 35 56 37 33 | 22 35 26 46 11 18 
| 83 | 81 77 | 79 56 60 37 | 70 62 42 33 35 56 18 33 | 22 35 26 46 11 
| 81 | 79 77 | 70 56 60 37 | 35 62 42 33 35 56 18 33 | 22 11 26 46 
| 79 | 70 77 | 62 56 60 37 | 35 46 42 33 35 56 18 33 | 22 11 26 
| 77 | 70 60 | 62 56 56 37 | 35 46 42 33 35 26 18 33 | 22 11 
| 70 | 62 60 | 46 56 56 37 | 35 11 42 33 35 26 18 33 | 22 
| 62 | 56 60 | 46 42 56 37 | 35 11 22 33 35 26 18 33 
| 60 | 56 56 | 46 42 35 37 | 35 11 22 33 33 26 18 
| 56 | 46 56 | 35 42 35 37 | 18 11 22 33 33 26 
| 56 | 46 37 | 35 42 35 26 | 18 11 22 33 33 
| 46 | 42 37 | 35 33 35 26 | 18 11 22 33 
| 42 | 35 37 | 33 33 35 26 | 18 11 22 
| 37 | 35 35 | 33 33 22 26 | 18 11 
| 35 | 33 35 | 18 33 22 26 | 11 
| 35 | 33 26 | 18 33 22 11 
| 33 | 33 26 | 18 11 22 
| 33 | 22 26 | 18 11 
| 26 | 22 11 | 18 
| 22 | 18 11 
| 18 | 11 
| 11