Advertisement
Guest User

Untitled

a guest
May 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. // HeapSort.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //This program encrypt every step of algoritm. And after that decrypt our first permutation
  3. #include <iostream>
  4. #include <string>
  5. #define N 1000000
  6. using namespace std;
  7.  
  8. struct per{
  9. char l;
  10. char r;
  11. };
  12. struct mov {
  13. per* P = new per[N];
  14. int top;
  15. };
  16. void push(mov *M, char l, char r) {
  17. M->top++;
  18. if (M->top >= N) {
  19. cout << "Sturcture is FULL" << endl;
  20. return ;
  21. }
  22. M->P[M->top].l = l;
  23. M->P[M->top].r = r;
  24.  
  25. }
  26. void pop(mov *M , char* l, char* r) {
  27. if (M->top < 0) {
  28. cout << "End of structure" << endl;
  29. }
  30. else {
  31.  
  32. *l = M->P[M->top].l;
  33. *r = M->P[M->top].r;
  34. M->top--;
  35. }
  36.  
  37. }
  38. bool full(mov* M) {
  39. if (M->top < 0) {
  40. return false;
  41. }
  42. return true;
  43. }
  44. void display(mov *M, char* l, char* r) {
  45. *l = M->P[M->top].l;
  46. *r = M->P[M->top].r;
  47. }
  48. //----------------------------------------------------------
  49. /* A utility function to print array of size n */
  50. void printArray(int arr[], int n)
  51. {
  52. for (int i = 0; i < n; ++i)
  53. cout << arr[i] << " ";
  54. cout << "\n";
  55. }
  56.  
  57. // To heapify a subtree rooted with node i which is
  58. // an index in arr[]. n is size of heap
  59. void buildMaxHeap(mov * M,int arr[], int n)
  60. {
  61. for (int i = 1; i < n; i++)
  62. { // d > T
  63. if (arr[i] > arr[(i - 1) / 2])
  64. {
  65. int j = i;
  66. while (arr[j] > arr[(j - 1) / 2])
  67. {
  68. printArray(arr, n);
  69. if (j % 2 == 1) {
  70. push(M, '0', '0');
  71. }
  72. else {
  73. push(M, '0', '1');
  74. }
  75. swap(arr[j], arr[(j - 1) / 2]);
  76. j = (j - 1) / 2;
  77. }
  78. }
  79. push(M, '1', '0');
  80. }
  81. }
  82.  
  83. void heapSort(mov * M, int arr[], int n)
  84. {
  85. bool log = false;
  86. buildMaxHeap(M, arr, n);
  87. for (int i = n - 1; i > 0; i--)
  88. {
  89. swap(arr[0], arr[i]);
  90. printArray(arr, n);
  91. int root = 0, child;
  92. do{
  93. log = false;
  94. child = (2 * root + 1);
  95. if (arr[child] < arr[child + 1] && child < (i - 1))
  96. child++;
  97.  
  98. if (arr[root] < arr[child] && child < i) {
  99. if (child % 2 == 1) {//lewe dziecko
  100. push(M, '0', '0');
  101. }
  102. else {
  103. push(M, '0', '1');
  104. }
  105. swap(arr[root], arr[child]);
  106. }
  107. if((arr[root] < arr[child]) ) {
  108. push(M, '1', '0'); // Bit STOP
  109. log = true;
  110. }
  111. root = child;
  112. } while (child < i );
  113. if (log == false) {
  114. push(M, '1', '0'); // Bit STOP
  115. }
  116. }
  117. }
  118. void unheapSort(mov * M, int arr[], int n) {
  119. char l, r,k;
  120. //Czesc dla HeapSort for
  121. mov* H = new mov;
  122. H->top = -1;
  123. pop(M, &l, &r);
  124. for (int i = 1; i < n; i++) {
  125. int root = 0, child;
  126. pop(M, &l, &r);
  127. //Idziemy po sciezce do napotkania bitu stop
  128. while (l == '0' && full(M)) {
  129. push(H, l, r);
  130. pop(M, &l, &r);
  131. }//While
  132.  
  133. while (full(H)) {
  134. pop(H, &l, &r);
  135. if (l == '0' && r == '0') {
  136. root = 2 * root + 1;
  137. }
  138. if (l == '0' && r == '1') {
  139. root = 2 * root + 2;
  140. }
  141. }
  142. //Robimy MIN dla elementu root;
  143. child = root;
  144. while (root > 0) {
  145. root = (child - 1) / 2;
  146. swap(arr[root], arr[child]);
  147. child = root;
  148. }
  149. swap(arr[0], arr[i]);
  150.  
  151. }//For
  152.  
  153. cout << "Befor MAX ";
  154. printArray(arr, n);
  155.  
  156. //czesc dla MaxHeap
  157. mov * K = new mov;
  158. K->top = -1;
  159. mov* R = new mov;
  160. R->top = -1;
  161. for (int i = n -1; i >= 0; i--) {
  162. int root = i, child;
  163. pop(M, &l, &r);
  164. while (l == '0' && full(M)) {
  165. push(K, l, r);
  166. pop(M, &l, &r);
  167. }//While
  168. while (full(K)) {
  169. pop(K, &l, &r);
  170. push(R, l, r);
  171. root = (root - 1) / 2;
  172. }
  173. while (root < i) {
  174. pop(R, &l, &r);
  175. if (l == '0' && r == '0') {
  176. swap(arr[root], arr[root * 2 + 1]);
  177. root = 2 * root + 1;
  178. }
  179. if (l == '0'&& r == '1') {
  180. swap(arr[root], arr[root * 2 + 2]);
  181. root = root * 2 + 2;
  182. }
  183. }
  184. //Robimy szos dla elementu root;
  185. printArray(arr, n);
  186. }
  187. }
  188.  
  189. // Driver program
  190. int main()
  191. {
  192. char s, l, r;
  193. mov * A = new mov;
  194. A->top = -1;
  195. push(A, '1', '0');
  196.  
  197. // int arr[] = { 12, 11, 13, 5, 6, 7,2,9,14,8,4 };
  198. // int arr[] = { 12, 11, 13, 5, 6, 7,2,9,14,8,4,78 };
  199. int arr[] = { 43,26,45,17,19,31,1,4,143,61,37,1 };
  200.  
  201. int n = sizeof(arr) / sizeof(arr[0]);
  202.  
  203. heapSort(A,arr, n);
  204.  
  205. cout << "Sorted array is \n";
  206. printArray(arr, n);
  207. cout << endl;
  208. unheapSort(A, arr, n);
  209. cout << endl << "Our permutation is : " <<endl;
  210. printArray(arr, n);
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement