Vlad5080

2nd practice

Mar 25th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdlib.h>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int i, j, n, d, count;
  8. void Shell(int* x, int n) {
  9. unsigned long int M = 0, C = 0;
  10. d = n;
  11. d = d / 2;
  12. while (d > 0) {
  13. for (i = 0; i < n - d; i++) {
  14. j = i;
  15. C++;
  16. while (j >= 0 && x[j] > x[j + d]) {
  17. C++;
  18. swap(x[j], x[j + d]);
  19. j--;
  20. M++;
  21. }
  22. }
  23. d = d / 2;
  24. }
  25. if (n != 10) {
  26. cout << "C = " << C << " M = " << M << "\n";
  27. cout << "M + C = " << C + M << endl;
  28. }
  29. }
  30.  
  31. void BadSort(int* x, int n) {
  32. unsigned int C = 0, M = 0;
  33. d = n;
  34. d = d / d;
  35. while (d > 0) {
  36. for (i = 0; i < n - d; i++) {
  37. j = i;
  38. C++;
  39. while (j >= 0 && x[j] > x[j + d]) {
  40. C++;
  41. int count = x[j];
  42. x[j] = x[j + d];
  43. x[j + d] = count;
  44. M++;
  45. j--;
  46. }
  47. }
  48. d = d / 2;
  49. }
  50. if (n != 10) {
  51. cout << "C = " << C << " M = " << M << "\n";
  52. cout << "M + C = " << C + M << endl;
  53. }
  54. }
  55.  
  56. void test(int* x, int n) {
  57. for (int i = 0; i < 10; i++) {
  58. cin >> x[i];
  59. }
  60. }
  61.  
  62. void fillarr(int* x, int* a, int n) {
  63. for (int i = 0; i < n; i++) x[i] = rand();
  64. for (int i = 0; i < n; i++) a[i] = x[i];
  65. }
  66.  
  67. int main() {
  68. setlocale(0, "rus");
  69. const int t = 10, n1 = 60000, n2 = 70000, n3 = 80000, n4 = 90000, n5 = 100000;
  70. srand(time(0));
  71. int x[t], x1[n1], x2[n2], x3[n3], x4[n4], x5[n5], a1[n1], a2[n2], a3[n3], a4[n4], a5[n5];
  72.  
  73. cout << "Заполните тестовый массив:\n";
  74. test(x, t);
  75. cout << "Введённый массив:\n";
  76. for (int i = 0; i < 10; i++) {
  77. cout << x[i] << " ";
  78. }
  79. cout << endl;
  80. Shell(x, 10);
  81. cout << "Отсортированный массив:\n";
  82. for (int i = 0; i < 10; i++) {
  83. cout << x[i] << " ";
  84. }
  85. cout << endl;
  86.  
  87. fillarr(x1, a1, n1);// заполнение массивов для проведения работы
  88. fillarr(x2, a2, n2);
  89. fillarr(x3, a3, n3);
  90. fillarr(x4, a4, n4);
  91. fillarr(x5, a5, n5);// конец заполнения массивов для работы
  92.  
  93. cout << "--------------------------\nn1=60000\nСредний случай\n";
  94. double t1 = clock();
  95. Shell(x1, n1);
  96. double t2 = clock();
  97. double t3 = t2 - t1;
  98. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  99. cout << "Наилучший случай\n";
  100. t1 = clock();
  101. BadSort(x1, n1);
  102. t2 = clock();
  103. t3 = t2 - t1;
  104. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  105. cout << "Наихудший случай\n";
  106. t1 = clock();
  107. BadSort(a1, n1);
  108. t2 = clock();
  109. t3 = t2 - t1;
  110. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  111. cout << "--------------------------\nn2=70000\nСредний случай\n";
  112. t1 = clock();
  113. Shell(x2, n2);
  114. t2 = clock();
  115. t3 = t2 - t1;
  116. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  117. cout << "Наилучший случай\n";
  118. t1 = clock();
  119. BadSort(x2, n2);
  120. t2 = clock();
  121. t3 = t2 - t1;
  122. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  123. cout << "Наихудший случай\n";
  124. t1 = clock();
  125. BadSort(a2, n2);
  126. t2 = clock();
  127. t3 = t2 - t1;
  128. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  129. cout << "--------------------------\nn3=80000\nСредний случай\n";
  130. t1 = clock();
  131. Shell(x3, n3);
  132. t2 = clock();
  133. t3 = t2 - t1;
  134. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  135. cout << "Наилучший случай\n";
  136. t1 = clock();
  137. BadSort(x3, n3);
  138. t2 = clock();
  139. t3 = t2 - t1;
  140. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  141. cout << "Наихудший случай\n";
  142. t1 = clock();
  143. BadSort(a3, n3);
  144. t2 = clock();
  145. t3 = t2 - t1;
  146. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  147. cout << "--------------------------\nn4=90000\nСредний случай\n";
  148. t1 = clock();
  149. Shell(x4, n4);
  150. t2 = clock();
  151. t3 = t2 - t1;
  152. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  153. cout << "Наилучший случай\n";
  154. t1 = clock();
  155. BadSort(x4, n4);
  156. t2 = clock();
  157. t3 = t2 - t1;
  158. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  159. cout << "Наихудший случай\n";
  160. t1 = clock();
  161. BadSort(a4, n4);
  162. t2 = clock();
  163. t3 = t2 - t1;
  164. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  165. cout << "--------------------------\nn5=100000\nСредний случай\n";
  166. t1 = clock();
  167. Shell(x5, n5);
  168. t2 = clock();
  169. t3 = t2 - t1;
  170. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  171. cout << "Наилучший случай\n";
  172. t1 = clock();
  173. BadSort(x5, n5);
  174. t2 = clock();
  175. t3 = t2 - t1;
  176. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  177. cout << "Наихудший случай\n";
  178. t1 = clock();
  179. BadSort(a5, n5);
  180. t2 = clock();
  181. t3 = t2 - t1;
  182. cout << "T(n) = " << t3 / CLOCKS_PER_SEC << " c" << endl;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment