Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define e 0.000001
  5.  
  6. int comp = 0; //количество сравнений
  7. int mov = 0; //количество перемещений
  8.  
  9. void swap (double *a, double *b) {
  10. double t = *a;
  11. *a = *b;
  12. *b = t;
  13. }
  14.  
  15. double absd (double a) { //модуль от double
  16. if (a < -e)
  17. return -a;
  18. return a;
  19.  
  20. }
  21.  
  22. void gen (int n, int type, double **p1, double **p2) { //генератор элементов по типу
  23. double *t1 = *p1;
  24. double *t2 = *p2;
  25. long long ran = rand();
  26. if (type == 1)
  27. for (int i = 1; i <= n; i++) {
  28. t1++;
  29. t2++;
  30. *t1 = i / ran;
  31. *t2 = i / ran;
  32. }
  33. else if (type == 2)
  34. for (int i = n; i > 0; i--) {
  35. t1--;
  36. t2--;
  37. *t1 = i / ran;
  38. *t2 = i / ran;
  39. }
  40. else
  41. for (int i = 0; i < n; i++) {
  42. t1++;
  43. t2++;
  44. *t1 = (double) (rand() * rand() * rand() * rand() * rand()) / 1000000;
  45. *t1 *= (double) rand();
  46. *t2 = *t1;
  47. }
  48. }
  49.  
  50. void bubble (double *a, int n) { //пузырьковая сортировка
  51. for (int i = 0; i < n; i++) {
  52. for (int j = i; j < n - 1; j++) {
  53. comp++;
  54. if (absd(a[j]) > absd(a[i])) {
  55. mov++;
  56. swap(&a[j], &a[i]);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. void heapify(double *a, int i, int n) { //просеивание элемента
  63. int root = i;
  64. int l = 2 * i + 1;
  65. int r = 2 * i + 2;
  66. comp++;
  67. if (l < n && absd(a[l]) < absd(a[root])) {
  68. root = l;
  69. }
  70. comp++;
  71. if (r < n && absd(a[r]) < absd(a[root])) {
  72. root = r;
  73. }
  74. if (root != i) {
  75. mov++;
  76. swap(&a[i], &a[root]);
  77. heapify(a, root, n);
  78. }
  79. }
  80.  
  81. void build_heap(double *a, int n) { //построение кучи
  82. for (int i = n / 2; i >= 0; i--)
  83. heapify(a, i, n);
  84. }
  85.  
  86. void heap_sort(double *a, int n) { //пирамидальная сортировка
  87. build_heap(a, n);
  88. int k = n - 1;
  89. for (int i = k; i > 0; i--) {
  90. mov++;
  91. swap(&a[i], &a[0]);
  92. n--;
  93. heapify(a, 0, n);
  94. }
  95. }
  96.  
  97. void check_sort(double *a, int n) {
  98. int flag = 1;
  99. for(int i = 1; i < n; i++) {
  100. if(a[i] - a[i-1] > e)
  101. }
  102. }
  103.  
  104. int main(void) {
  105. int n, type;
  106. scanf("%d%d", &n, &type);
  107. double *a1;
  108. double *a2;
  109. a1 = (double *)malloc(n * sizeof(double) + 10);
  110. a2 = (double *)malloc(n * sizeof(double) + 10);
  111. gen(n, type, &a1, &a2);
  112. bubble(a1, n);
  113. printf("%d %d\n", comp, mov);
  114. comp = 0;
  115. mov = 0;
  116. heap_sort(a2, n);
  117. printf("%d %d\n", comp, mov);
  118. for (int i = 0; i < n; i++)
  119. printf("%lf ", a1[i]);
  120. printf("\n");
  121. for (int i = 0; i < n; i++)
  122. printf("%lf ", a2[i]);
  123.  
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement