Advertisement
Guest User

Untitled

a guest
May 27th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <random>
  5.  
  6. using namespace std;
  7.  
  8. /*template <typename T>
  9. class CTablice
  10. {
  11. public:
  12. T *tab;
  13. CTablice()
  14. {
  15. tab = new int[n] {};
  16. }
  17. int shake_sort();
  18. void quick_sort_H();
  19.  
  20. };*/
  21.  
  22. class CSort {
  23. public:
  24.  
  25. int nElements;
  26.  
  27. int* randomTable(int size) {
  28. int *T = nullptr;
  29. try {
  30. T = new int[size];
  31. }
  32. catch (std::bad_alloc) {
  33. getchar();
  34. std::cin.ignore();
  35. exit(0);
  36. }
  37. random_device rd;
  38. mt19937 mt(rd());
  39. uniform_int_distribution<int>dist(-1000, 2147483647);
  40. for (int i = 0; i < size; ++i) {
  41. T[i] = dist(mt);
  42. }
  43. return T;
  44. }
  45.  
  46. int* growingTable(int size) {
  47. int *T = nullptr;
  48. try {
  49. T = new int[size];
  50. }
  51. catch (std::bad_alloc) {
  52. getchar();
  53. std::cin.ignore();
  54. exit(0);
  55. }
  56. for (int i = 0; i < size; i++) {
  57. T[i] = i;
  58. }
  59. return T;
  60. }
  61.  
  62. int* decreasingTable(int size) {
  63. int *T = nullptr;
  64. try {
  65. T = new int[size];
  66. }
  67. catch (std::bad_alloc) {
  68. getchar();
  69. std::cin.ignore();
  70. exit(0);
  71. }
  72. for (int i = 0; i < size; i++) {
  73. T[i] = size - i;
  74. }
  75. return T;
  76. }
  77.  
  78. int* smallPartShakedTable(int size) {
  79. int *T = nullptr;
  80. try {
  81. T = new int[size];
  82. }
  83. catch (std::bad_alloc) {
  84. getchar();
  85. std::cin.ignore();
  86. exit(0);
  87. }
  88. for (int i = 0; i < size; i++) {
  89. if (i % 10 == 0) {
  90. T[i] = size - i;
  91. }
  92. else T[i] = i;
  93. }
  94. return T;
  95. }
  96. };
  97.  
  98. class CTablica:CSort
  99. {
  100. public:
  101. int *tab{ nullptr };
  102. int l_porownan, l_inwersji;
  103. CTablica(int number, int n)
  104. {
  105. switch (number)
  106. {
  107. case 1:
  108. {
  109. tab = randomTable(n);
  110. break;
  111. }
  112. case 2:
  113. {
  114. tab = growingTable(n);
  115. break;
  116. }
  117. case 3:
  118. {
  119. tab = decreasingTable(n);
  120. break;
  121. }
  122. case 4 :
  123. {
  124. tab = smallPartShakedTable(n);
  125. break;
  126. }
  127. }
  128. }
  129.  
  130. void swap(int i, int j)
  131. {
  132. int temp = tab[i];
  133. tab[i] = tab[j];
  134. tab[j] = temp;
  135. }
  136.  
  137. void show(int n)
  138. {
  139. for (int i = 0; i < n; i++)
  140. {
  141. cout << tab[i] << " ";
  142. }
  143. cout << "\nLiczba inwersji: " << l_inwersji << endl;
  144. cout << "Liczba porownan: " << l_porownan << endl;
  145. }
  146. };
  147.  
  148. class ShakeSort:public CTablica
  149. {
  150. public:
  151. ShakeSort(int number, int n) :CTablica(number,n) {};
  152. void shake_sort(ShakeSort *T, int n);
  153.  
  154. };
  155.  
  156. class CoctailSort :public CTablica
  157. {
  158. public:
  159. CoctailSort(int number, int n) :CTablica(number, n) {};
  160. void cocktailSort(CoctailSort *T, const int lowestIndex, const int highestIndex);
  161. };
  162.  
  163. class Loumoto:public CTablica
  164. {
  165. public:
  166. Loumoto(int number, int n) :CTablica(number, n) {};
  167. void quickSort_L(Loumoto *T, int lowestIndex, int highestIndex);
  168. };
  169.  
  170. class Hoare :public CTablica
  171. {
  172. public:
  173. Hoare(int number, int n) :CTablica(number, n) {};
  174. void quick_sort_H(Hoare *T, int low, int high);
  175. };
  176.  
  177. class Tree :public CTablica
  178. {
  179. public:
  180. Tree(int number, int n) :CTablica(number, n) {};
  181. void sortHeap(Tree *T, const int lowestIndex, const int highestIndex);
  182. };
  183.  
  184. //Coctail sort
  185. void CoctailSort::cocktailSort(CoctailSort *T, const int lowestIndex, const int highestIndex)
  186. {
  187. int len = highestIndex - lowestIndex + 1;
  188. bool swapped = true;
  189. while (swapped) {
  190. for (int i = 0; i < len - 1; i++) {
  191. T->l_porownan++;
  192. if (T->tab[i] > T->tab[i+1]) {
  193. T->swap(i, i+1);
  194. T->l_inwersji++;
  195. swapped = true;
  196. }
  197. }
  198. if (swapped = false) {
  199. break;
  200. }
  201. swapped = false;
  202. for (int i = len - 1; i > 0; i--) {
  203. T->l_porownan++;
  204. if (T->tab[i-1] > T->tab[i]) {
  205. T->l_inwersji++;
  206. T->swap(i, i-1);
  207. swapped = true;
  208. }
  209. }
  210. }
  211. }
  212.  
  213.  
  214. //Loumoto sort
  215. /*
  216. po wyznaczeniu skrajnego elementu tablicy (z prawej) jako pivota, musimy wyznaczyć jego właściwe położenie. Do tego indeksy: i oraz j.
  217. Porównujemy kolejno wszystkie elementy od lewej z pivotem. Służy nam do tego zmienna j oznaczająca indeks tablicowy porównywanego aktualnie elementu
  218. */
  219. int partitionLomuto(Loumoto *T, const int lowestIndex, const int highestIndex) {
  220. int pivot = T->tab[highestIndex];
  221. int swapIndex = lowestIndex - 1; //place for swaping
  222. for (int i = lowestIndex; i < highestIndex; i++)
  223. {
  224. T->l_porownan++;
  225. if (T->tab[i] <= pivot)
  226. {
  227. swapIndex++;
  228. T->swap(swapIndex, i);
  229. T->l_inwersji++;
  230. }
  231. }
  232. T->swap(swapIndex + 1, highestIndex);
  233. T->l_inwersji++;
  234. return swapIndex + 1;
  235. }
  236.  
  237. void Loumoto::quickSort_L(Loumoto *T, int lowestIndex, int highestIndex) {
  238. if (lowestIndex < highestIndex) {
  239.  
  240. int partitionIndex = partitionLomuto(T, lowestIndex, highestIndex);
  241.  
  242. quickSort_L(T, lowestIndex, partitionIndex - 1);
  243. quickSort_L(T, partitionIndex + 1, highestIndex);
  244.  
  245. }
  246. }
  247.  
  248. //Tree sort
  249. //poddrzewo zakorzenione w wezle
  250. void subTree(Tree *T, const int first, const int last)
  251. {
  252. int toSwap = last;
  253. int leftIndex = 2 * last + 1;
  254. int rightIndex = leftIndex + 1;
  255.  
  256. //gdy lewe dziecko wieksze od korzenia(tego od czego wychodzi)
  257. T->l_porownan++;
  258. if (leftIndex<first && T->tab[leftIndex]>T->tab[toSwap])
  259. {
  260. toSwap = leftIndex;
  261. }
  262.  
  263. //gdy prawe -//-
  264. T->l_porownan++;
  265. if (rightIndex<first && T->tab[rightIndex]>T->tab[toSwap])
  266. {
  267. toSwap = rightIndex;
  268. }
  269.  
  270. if (toSwap != last) {
  271. T->swap(last, toSwap);
  272. T->l_inwersji++;
  273. subTree(T, first, toSwap);
  274. }
  275. }
  276.  
  277. void Tree::sortHeap(Tree *T, const int lowestIndex, const int highestIndex)
  278. {
  279. for (int i = (highestIndex / 2) - 1; i >= 0; i--) {
  280. subTree(T, i, highestIndex);
  281. }
  282.  
  283. for (int i = highestIndex - 1; i >= 0; i--) {
  284. T->swap(0, i);
  285. subTree(T, i, 0);
  286. }
  287. }
  288.  
  289.  
  290.  
  291. // Benio
  292. //Shake sort
  293. void ShakeSort::shake_sort(ShakeSort *T, int n)
  294. {
  295. int l_inwersji{ 0 };
  296. for (int i = 0; i < n / 2; i++)
  297. {
  298. for (int j = i + 1; j < n; j++)
  299. {
  300. if (T->tab[j-1] > T->tab[j])
  301. {
  302. T->l_porownan++;
  303. T->swap(j, j - 1);
  304. T->l_inwersji++;
  305. }
  306. }
  307. for (int k = n - i - 1; k > 0; k--)
  308. {
  309. if (T->tab[k-1] > T->tab[k])
  310. {
  311. T->l_porownan++;
  312. T->swap(k, k - 1);
  313. T->l_inwersji++;
  314. }
  315. }
  316. }
  317. }
  318.  
  319. //Hoare sort
  320. int partition(Hoare *T, int low, int high)
  321. {
  322. int pivot{ T->tab[low] };
  323. int i = low - 1, j = high + 1;
  324. while (true)
  325. {
  326. do {
  327. i++;
  328. T->l_porownan++;
  329. } while (T->tab[i] < pivot);
  330.  
  331. do {
  332. j--;
  333. T->l_porownan++;
  334. } while (T->tab[j] > pivot);
  335.  
  336. if (i >= j)
  337. return j;
  338.  
  339. T->swap(i, j);
  340. T->l_inwersji++;
  341. }
  342. }
  343.  
  344. void Hoare::quick_sort_H(Hoare *T, int low, int high)
  345. {
  346. if (low < high)
  347. {
  348. int parti = partition(T, low, high);
  349. quick_sort_H(T, low, parti);
  350. quick_sort_H(T, parti + 1, high);
  351. }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement