Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <time.h>
  5. #include <math.h>
  6.  
  7. using namespace std;
  8.  
  9. int *sortedNumbers(int n)
  10. {
  11. int *table = new int[n];
  12. for (int i = 0; i < n; ++i)
  13. table[i] = i;
  14. return table;
  15. }
  16.  
  17. int *randomNumbers(int n)
  18. {
  19. int *table = new int[n];
  20. for (int i = 0; i < n; ++i)
  21. table[i] = rand();
  22. return table;
  23. }
  24.  
  25. void swap(int &a, int &b)
  26. {
  27. int tmp = a;
  28. a = b;
  29. b = tmp;
  30. }
  31.  
  32. int *partlyRandomNumbers(int n, int m)
  33. {
  34. int *table = sortedNumbers(n);
  35. for (int i = 0; i < m; ++i)
  36. {
  37. int ii = rand() % n;
  38. int jj = rand() % n;
  39. while (ii != jj)
  40. jj = rand() % n;
  41. swap(table[ii], table[jj]);
  42. }
  43. return table;
  44. }
  45.  
  46. typedef void(*Sortowanie)(int *tab, int ilosc, int &licznik1, int &licznik2);
  47.  
  48. void sortowanie1(int *tab, int ilosc, int &porownania, int &zamiany)
  49. {
  50. //Sortowanie babelkowe
  51. porownania = 0;
  52. zamiany = 0;
  53.  
  54. for (int j = 0; j<ilosc - 1; j++)
  55. {
  56. for (int i = 0; i<ilosc - 1; i++)
  57. {
  58. if (tab[i]>tab[i + 1])
  59. {
  60. swap(tab[i], tab[i + 1]);
  61. zamiany++;
  62. }
  63. porownania++;
  64.  
  65. }
  66. }
  67. }
  68.  
  69. void sortowanie2(int *tab, int ilosc, int &porownania, int &zamiany)
  70. {
  71. //Sortowanie przez kopcowanie
  72. porownania = 0;
  73. zamiany = 0;
  74. int i, j, k, m, x;
  75.  
  76. for (i = 2; i <= ilosc; i++)
  77. {
  78. j = i; k = j / 2;
  79. x = tab[i];
  80. while ((k > 0) && (tab[k] < x))
  81. {
  82. tab[j] = tab[k];
  83. j = k; k = j / 2;
  84. porownania++;
  85. }
  86. tab[j] = x;
  87. }
  88.  
  89. for (i = ilosc; i>1; i--)
  90. {
  91. swap(tab[1], tab[i]);
  92. zamiany++;
  93. j = 1; k = 2;
  94.  
  95. while (k < i)
  96. {
  97. porownania++;
  98. if ((k + 1 < i) && (tab[k + 1] > tab[k]))
  99. {
  100. m = k + 1;
  101. }
  102. else
  103. m = k;
  104.  
  105. porownania++;
  106. if (tab[m] <= tab[j])
  107. {
  108. break;
  109. }
  110. swap(tab[j], tab[m]);
  111. zamiany++;
  112. j = m;
  113. k = j + j;
  114. porownania++;
  115. }
  116. }
  117. }
  118.  
  119. void sortowanie3(int *tab, int ilosc, int &porownania, int &zamiany)
  120. {
  121. //Sortowanie przez scalanie
  122. porownania = 0;
  123. zamiany = 0;
  124.  
  125.  
  126. }
  127.  
  128. void sortowanie4(int *tab, int ilosc, int &porownania, int &zamiany)
  129. {
  130. //Sortowanie quicksort
  131. porownania = 0;
  132. zamiany = 0;
  133.  
  134.  
  135. }
  136.  
  137. void sortowanie5(int *tab, int ilosc, int &porownania, int &zamiany)
  138. {
  139. //Sortowanie przez wybór
  140. porownania = 0;
  141. zamiany = 0;
  142. int j, pmin;
  143.  
  144. for (j = 0; j < ilosc - 1; j++)
  145. {
  146. pmin = j;
  147. for (int i = j + 1; i < ilosc; i++)
  148. {
  149. porownania++;
  150. if (tab[i] < tab[pmin])
  151. {
  152. pmin = i;
  153. }
  154. swap(tab[pmin], tab[j]);
  155. zamiany++;
  156. }
  157. }
  158. }
  159.  
  160. void sortowanie6(int *tab, int ilosc, int &porownania, int &zamiany)
  161. {
  162. //Sortowanie przez wstawianie
  163. porownania = 0;
  164. zamiany = 0;
  165. int i, j, x;
  166. for (j = ilosc - 2; j >= 0; j--)
  167. {
  168. x = tab[j];
  169. i = j + 1;
  170. while ((i < ilosc) && (x > tab[i]))
  171. {
  172. tab[i - 1] = tab[i];
  173. i++;
  174. }
  175. porownania++;
  176. tab[i - 1] = x;
  177. }
  178. }
  179.  
  180. int main()
  181. {
  182. int *table1 = sortedNumbers(50);
  183. int *table2 = sortedNumbers(500);
  184. int *table3 = sortedNumbers(2000);
  185.  
  186. int *table4 = randomNumbers(50);
  187. int *table5 = randomNumbers(500);
  188. int *table6 = randomNumbers(2000);
  189.  
  190. int *table7 = partlyRandomNumbers(50, 10);
  191. int *table8 = partlyRandomNumbers(500, 50);
  192. int *table9 = partlyRandomNumbers(2000, 200);
  193.  
  194. int* tables[9] = {
  195. table1, table2, table3,
  196. table4, table5, table6,
  197. table7, table8, table9
  198. };
  199.  
  200. int ilosci[9] = {
  201. 50, 500, 2000,
  202. 50, 500, 2000,
  203. 50, 500, 2000
  204. };
  205.  
  206. Sortowanie funkcje[6] = {
  207. sortowanie1,
  208. sortowanie2,
  209. sortowanie3,
  210. sortowanie4,
  211. sortowanie5,
  212. sortowanie6
  213. };
  214.  
  215. for (int j = 0; j < 6; ++j)
  216. {
  217. cout << endl << "Sortowanie " << (j + 1) << ":" << endl;
  218. cout << "\tPorownania:\tZamiany:\tCzas dzialania:" << endl;
  219.  
  220. for (int i = 0; i < 9; ++i)
  221. {
  222. int porownania = 0;
  223. int zamiany = 0;
  224.  
  225. Sortowanie sortowanie = funkcje[j];
  226.  
  227. clock_t t1 = clock();
  228. sortowanie(tables[i], ilosci[i], porownania, zamiany);
  229. clock_t t2 = clock();
  230.  
  231. double czas = ((double)(t2 - t1)) / CLOCKS_PER_SEC;
  232.  
  233. cout << i+1 << ".\t" << porownania << "\t\t" << zamiany << "\t\t" << czas << endl;
  234. }
  235. }
  236.  
  237. /*for (int i = 0; i < 50; i++)
  238. {
  239. cout << table1[i] << " ";
  240. }*/
  241.  
  242. system("pause");
  243. return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement