Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <Windows.h>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. // Сортировка вставками
  8. void InsertionSort(int*& array, int size, long long int& comp, long long int& assign)
  9. {
  10. comp = assign = 0;
  11.  
  12. for (int j = 1; j < size; j++)
  13. {
  14. int i = j;
  15.  
  16. while (++comp && array[i - 1] > array[i] && i > 0)
  17. {
  18. swap(array[i], array[i - 1]);
  19. assign += 2;
  20. i--;
  21. }
  22. }
  23. }
  24.  
  25. // Сортировка слиянием
  26. void MergeSort(int*& array1, int size, long long int& comp, long long int& assign)
  27. {
  28. comp = assign = 0;
  29. int* array2 = new int[size];
  30. bool sorted = false;
  31. while (!sorted)
  32. {
  33. bool odd = true;
  34. int i = 1, j = size - 1, k = 1;
  35. array2[0] = array1[0];
  36.  
  37. while (k < size)
  38. {
  39. comp++;
  40.  
  41. if (array1[k] >= array1[k - 1])
  42. {
  43. assign++;
  44.  
  45. if (odd)
  46. {
  47. array2[i++] = array1[k];
  48. }
  49.  
  50. else
  51. {
  52. array2[j--] = array1[k];
  53. }
  54. }
  55.  
  56. else
  57. {
  58. assign++;
  59.  
  60. odd = !odd;
  61.  
  62. if (odd)
  63. {
  64. array2[i++] = array1[k];
  65. }
  66.  
  67. else
  68. {
  69. array2[j--] = array1[k];
  70. }
  71. }
  72. k++;
  73. }
  74. bool series1 = true, series2 = true;
  75. k = i = 0; j = size - 1;
  76.  
  77. while (k < size)
  78. {
  79. comp++;
  80.  
  81. if (array2[i] <= array2[j] && series1 || !series2)
  82. {
  83. assign++;
  84. array1[k++] = array2[i++];
  85.  
  86. if (array2[i] < array2[i - 1] && series1)
  87. {
  88. series1 = false;
  89. }
  90. }
  91.  
  92. if (k == size)
  93. {
  94. break;
  95. }
  96.  
  97. if (!series1 && !series2)
  98. {
  99. series1 = series2 = true;
  100. }
  101. comp++;
  102.  
  103. if (array2[j] <= array2[i] && series2 || !series1)
  104. {
  105. assign++;
  106. array1[k++] = array2[j--];
  107.  
  108. if (array2[j] < array2[j + 1] && series2)
  109. {
  110. series2 = false;
  111. }
  112. }
  113.  
  114. if (!series1 && !series2)
  115. {
  116. series1 = series2 = true;
  117. }
  118. }
  119.  
  120. sorted = true;
  121.  
  122. for (i = 1; i < size; i++)
  123. {
  124. if (array1[i] < array1[i - 1])
  125. {
  126. sorted = false;
  127. break;
  128. }
  129. }
  130. }
  131. }
  132.  
  133. int main()
  134. {
  135. ofstream fout("output.txt");
  136. setlocale(LC_ALL, "Russian");
  137. srand(GetTickCount());
  138. long long int comp = 0, assign = 0;
  139. auto start = chrono::high_resolution_clock::now();
  140. auto end = chrono::high_resolution_clock::now();
  141.  
  142. int* inc1000array1 = new int[1000], * inc1000array2 = new int[1000];
  143. for (int i = 0; i < 1000; i++)
  144. {
  145. inc1000array1[i] = inc1000array2[i] = i + 1;
  146. }
  147.  
  148. int* dec1000array1 = new int[1000], * dec1000array2 = new int[1000];
  149. for (int i = 1000; i > 0; i--)
  150. {
  151. dec1000array1[1000 - i] = dec1000array2[1000 - i] = i;
  152. }
  153.  
  154. int* rand100array1 = new int[100], * rand100array2 = new int[100];
  155. for (int i = 0; i < 100; i++)
  156. {
  157. rand100array1[i] = rand100array2[i] = rand();
  158. }
  159.  
  160. int* rand1000array1 = new int[1000], * rand1000array2 = new int[1000];
  161. for (int i = 0; i < 1000; i++)
  162. {
  163. rand1000array1[i] = rand1000array2[i] = rand();
  164. }
  165.  
  166. int* rand10000array1 = new int[10000], * rand10000array2 = new int[10000];
  167. for (int i = 0; i < 10000; i++)
  168. {
  169. rand10000array1[i] = rand10000array2[i] = rand();
  170. }
  171.  
  172. int* rand100000array1 = new int[100000], * rand100000array2 = new int[100000];
  173. for (int i = 0; i < 100000; i++)
  174. {
  175. rand100000array1[i] = rand100000array2[i] = rand();
  176. }
  177.  
  178. fout << "Числа 1 от 1000" << endl;
  179. fout << "Алгоритм простых вставок" << endl;
  180. start = chrono::high_resolution_clock::now();
  181. InsertionSort(inc1000array1, 1000, comp, assign);
  182. end = chrono::high_resolution_clock::now();
  183. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  184. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl;
  185. fout << "Алгоритм естественного слияния" << endl;
  186. start = chrono::high_resolution_clock::now();
  187. MergeSort(inc1000array2, 1000, comp, assign);
  188. end = chrono::high_resolution_clock::now();
  189. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  190. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl << endl;
  191.  
  192. fout << "Числа от 1000 до 1" << endl;
  193. fout << "Алгоритм простых вставок" << endl;
  194. start = chrono::high_resolution_clock::now();
  195. InsertionSort(dec1000array1, 1000, comp, assign);
  196. end = chrono::high_resolution_clock::now();
  197. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  198. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl;
  199. fout << "Алгоритм естественного слияния" << endl;
  200. start = chrono::high_resolution_clock::now();
  201. MergeSort(dec1000array2, 1000, comp, assign);
  202. end = chrono::high_resolution_clock::now();
  203. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  204. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl << endl;
  205.  
  206. fout << "100 случайных чисел" << endl;
  207. fout << "Алгоритм простых вставок" << endl;
  208. start = chrono::high_resolution_clock::now();
  209. InsertionSort(rand100array1, 100, comp, assign);
  210. end = chrono::high_resolution_clock::now();
  211. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  212. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl;
  213. fout << "Алгоритм естественного слияния" << endl;
  214. start = chrono::high_resolution_clock::now();
  215. MergeSort(rand100array2, 100, comp, assign);
  216. end = chrono::high_resolution_clock::now();
  217. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  218. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl << endl;
  219.  
  220. fout << "1000 случайных чисел" << endl;
  221. fout << "Алгоритм простых вставок" << endl;
  222. start = chrono::high_resolution_clock::now();
  223. InsertionSort(rand1000array1, 1000, comp, assign);
  224. end = chrono::high_resolution_clock::now();
  225. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  226. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl;
  227. fout << "Алгоритм естественного слияния" << endl;
  228. start = chrono::high_resolution_clock::now();
  229. MergeSort(rand1000array2, 1000, comp, assign);
  230. end = chrono::high_resolution_clock::now();
  231. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  232. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl << endl;
  233.  
  234. fout << "10000 случайных чисел" << endl;
  235. fout << "Алгоритм простых вставок" << endl;
  236. start = chrono::high_resolution_clock::now();
  237. InsertionSort(rand10000array1, 10000, comp, assign);
  238. end = chrono::high_resolution_clock::now();
  239. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  240. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl;
  241. fout << "Алгоритм естественного слияния" << endl;
  242. start = chrono::high_resolution_clock::now();
  243. MergeSort(rand10000array2, 10000, comp, assign);
  244. end = chrono::high_resolution_clock::now();
  245. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  246. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl << endl;
  247.  
  248. fout << "100000 случайных чисел" << endl;
  249. fout << "Алгоритм простых вставок" << endl;
  250. start = chrono::high_resolution_clock::now();
  251. InsertionSort(rand100000array1, 100000, comp, assign);
  252. end = chrono::high_resolution_clock::now();
  253. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  254. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl;
  255. fout << "Алгоритм естественного слияния" << endl;
  256. start = chrono::high_resolution_clock::now();
  257. MergeSort(rand100000array2, 100000, comp, assign);
  258. end = chrono::high_resolution_clock::now();
  259. fout << "Прошло " << chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " мкс" << endl;
  260. fout << "Сравнений - " << comp << ", присваиваний - " << assign << endl << endl;
  261.  
  262. return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement