Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct Heap {
  5. std::vector<int> saver;
  6.  
  7. void swap(int& first, int& second) {
  8. auto bufer = second;
  9. second = first;
  10. first = bufer;
  11. }
  12.  
  13. void sift_up(int index) {
  14. auto index_parent = (index-1)/2;
  15. while(index_parent >= 0){
  16. if( saver[index] > saver[index_parent]) {
  17. swap(saver[index], saver[index_parent]);
  18. index = index_parent;
  19. index_parent = (index-1)/2;
  20. } else {
  21. break;
  22. }
  23. }
  24. }
  25.  
  26. void sift_down(int index) {
  27. auto left_index = 2*index + 1;
  28. auto right_index = 2*index + 2;
  29.  
  30. while (TRUE) {
  31. if (left_index >= saver.size()) {
  32. break;
  33. } else if (right_index >= saver_size){
  34. auto change_index = left_index;
  35. } else if (saver[left_index] < saver[right_index]){
  36. auto change_index = left_index;
  37. } else {
  38. auto change_index = right_index;
  39. }
  40.  
  41. if (saver[change_index] > saver[index]) {
  42. swap(saver[change_index], saver[index]);
  43. index = change_index;
  44. } else {
  45. break;
  46. }
  47. }
  48. }
  49.  
  50. void insert(int x) {
  51. saver.push_back(x);
  52. auto x_index = saver.size() - 1;
  53. sift_up(x_index);
  54. }
  55.  
  56. int get_min() {
  57. return saver[0];
  58. }
  59.  
  60. void extract_min() {
  61. swap(saver[0],saver[saver.size()-1]);
  62. saver.pop_back();
  63. sift_down(0);
  64. }
  65.  
  66. void make_heap(std::vector<int> array) {
  67. int index = array.size()/2;
  68. while(index >= 0){
  69. sift_down(index);
  70. index = index - 1;
  71. }
  72. }
  73. }
  74.  
  75.  
  76. std::vector<int> psevdo_heap_sort(std::vector<int> array) {
  77. Heap myheap;
  78. for(int& el : array) {
  79. myheap.insert(el);
  80. }
  81.  
  82. std::vector<int> output_array;
  83. for(int i = 0; i < array.size(); ++i){
  84. output_array.push_back(myheap.get_min());
  85. }
  86. return output_array;
  87. }
  88.  
  89.  
  90. std::vector<int> heap_sort(std::vector<int> array) {
  91. Heap myheap;
  92. myheap.make_heap(array);
  93.  
  94. std::vector<int> output_array;
  95. for(int i = 0; i < array.size(); ++i){
  96. output_array.push_back(myheap.get_min());
  97. }
  98. return output_array;
  99. }
  100.  
  101.  
  102. int main() {
  103. int n_list;
  104. std::cin >> n_list;
  105. std::vector<int> list;
  106. int el;
  107. for (int i = 0; i < n_list; ++i) {
  108. std::cin >> el;
  109. list.push_back(el);
  110. }
  111.  
  112. auto output = psevdo_heap_sort(list);
  113. for (int i = 0; i < n_list; ++i) {
  114. std::cout << output[i];
  115. }
  116.  
  117. std::cout << '\n';
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement