Advertisement
Guest User

Untitled

a guest
May 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. // Permutations.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <ctime>
  8. #include <time.h>
  9. #include <chrono>
  10. std::fstream out;
  11. std::fstream in;
  12.  
  13. #define N 9
  14. #define MAX 70000 // Wielkosc stosu
  15. struct stack {
  16. int first[MAX];
  17. int second[MAX];
  18. int top;
  19. };
  20. typedef struct stack STACK;
  21. STACK s;
  22. void push(int x, int y) {
  23. if (s.top == MAX - 1) {
  24. std::cout << "Stack is FULL" << std::endl;
  25. return;
  26. }
  27. else {
  28. s.top += 1;
  29. s.first[s.top] = x;
  30. s.second[s.top] = y;
  31. }
  32. return;
  33. }
  34. void pop(int* x, int* y) {
  35. if (s.top == -1) {
  36. std::cout << "Stack is EMPTY" << std::endl;
  37. return;
  38. }
  39. else {
  40. *x = s.first[s.top];
  41. *y = s.second[s.top];
  42. s.top -= 1;
  43. }
  44. return;
  45. }
  46. bool isEmpty() {
  47. if (s.top == -1) {
  48. return true;
  49. }
  50. else {
  51. return false;
  52. }
  53. }
  54. int top() {
  55. return s.first[s.top];
  56. }
  57.  
  58. void swap(int* arr, int i, int j) {
  59. int k = arr[i];
  60. arr[i] = arr[j];
  61. arr[j] = k;
  62. }
  63. void Print(int* arr, int l) {
  64.  
  65. for (int i = 0; i < l; i++) {
  66. out << arr[i] << " ";
  67. }
  68. out << std::endl;
  69. }
  70. void Permutations(int* array, int* rezult, int length) {
  71. Print(array, length);
  72. int* brr = new int[length];
  73. for (int i = 0; i < length; i++) {
  74. brr[i] = 0;
  75. }
  76. int i = 1;
  77. while (i < length) {
  78. if (brr[i] < i) {
  79. int j = ((i % 2) == 0) ? 0 : brr[i];
  80. swap(array, i, j);
  81. Print(array, length);
  82. brr[i]++;
  83. i = 1;
  84. }
  85. else {
  86. brr[i] = 0;
  87. i++;
  88. }
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95. }
  96.  
  97. // Funkcje sortujace
  98. int partition(int* increase, int arr[], int low, int high) {
  99. int pivot = arr[high]; // pivot
  100. int i = (low - 1); // Index of smaller element
  101. *increase = *increase + 2;
  102. for (int j = low; j <= high - 1; j++)
  103. {
  104.  
  105. *increase = *increase + 1;
  106. if (arr[j] <= pivot)
  107. {
  108. i++;
  109. int k = arr[i];
  110. arr[i] = arr[j];
  111. arr[j] = k;
  112. *increase = *increase + 4;
  113. }
  114. }
  115. int k = arr[i + 1];
  116. arr[i + 1] = arr[high];
  117. arr[high] = k;
  118. *increase = *increase + 3;
  119. return (i + 1);
  120. }
  121. unsigned long int QuickSortIteretive(int arr[], int low, int high) {
  122. int increase = 0;
  123. while (true) {
  124. while (low < high) {
  125. int q = partition(&increase, arr, low, high);
  126. push((q + 1), high);
  127. high = q - 1;
  128. increase += 3;
  129. }
  130. if (isEmpty()) {
  131. break;
  132. }
  133. pop(&low, &high);
  134. increase++;
  135. }
  136. return increase;
  137. }
  138. unsigned long int BubleSort(int arr[]) {
  139. int i, j;
  140. int increase = 0;
  141. for (i = 0; i < N - 1; i++) {
  142. for (j = 0; j < N - i - 1; j++) {
  143. if (arr[j] > arr[j + 1]) {
  144. swap(arr, j, (j + 1));
  145. increase++;
  146. }
  147. }
  148. }
  149. return increase;
  150. }
  151. unsigned long int InsertionSort(int arr[]) {
  152. int i, key, j;
  153. int increase = 0;
  154. for (i = 1; i < N; i++)
  155. {
  156. key = arr[i];
  157. j = i - 1;
  158. increase += 2;
  159. while (j >= 0 && arr[j] > key)
  160. {
  161. arr[j + 1] = arr[j];
  162. j = j - 1;
  163. increase += 2;
  164. }
  165. arr[j + 1] = key;
  166. increase++;
  167. }
  168. return increase;
  169. }
  170. //===========================
  171.  
  172. void Testing_3_functions() {
  173. std::fstream outQuick;
  174. outQuick.open("Quicksort_Permutations.csv", std::ios::app);
  175.  
  176. std::fstream outBuble;
  177. outBuble.open("Bublesort_Permutations.csv", std::ios::app);
  178.  
  179. std::fstream outInsert;
  180. outInsert.open("Insertsort_Permutations.csv", std::ios::app);
  181.  
  182. int end = 1;
  183. for (int i = 1; i <= N; i++) {
  184. end *= i;
  185. }
  186. for (int i = 0; i < end; i++) {
  187. //Tablicy dla kazdego z sortowan
  188. int* quick = new int[N];
  189. int* buble = new int[N];
  190. int* insert = new int[N];
  191. int* rezult = new int[N]; // Tablica dla N losowych liczb
  192. int count = 0; // inkrement dla naszej tablicy rezult
  193. for (int i = 0; i < N; i++) {
  194. rezult[i] = -1;;
  195. }
  196. for (int j = 0; j < N; j++) {
  197. in >> rezult[j];
  198. //std::cout << rezult[j] << " ";
  199. }
  200.  
  201. in << std::endl;
  202. for (int j = 0; j < N; j++) {
  203. quick[j] = rezult[j];
  204. buble[j] = rezult[j];
  205. insert[j] = rezult[j];
  206. }
  207. //Nanosekundy
  208. //QUICK
  209. auto start_Q = std::chrono::high_resolution_clock::now();
  210. QuickSortIteretive(quick, 0, (N - 1));
  211. auto finish_Q = std::chrono::high_resolution_clock::now();
  212. int time_Q = std::chrono::duration_cast<std::chrono::nanoseconds>(finish_Q - start_Q).count() ;
  213. //BUBLE
  214. auto start_B = std::chrono::high_resolution_clock::now();
  215. BubleSort(buble);
  216. auto finish_B = std::chrono::high_resolution_clock::now();
  217. int time_B = std::chrono::duration_cast<std::chrono::nanoseconds>(finish_B - start_B).count();
  218.  
  219. //INSERT
  220. auto start_I = std::chrono::high_resolution_clock::now();
  221. InsertionSort(insert);
  222. auto finish_I = std::chrono::high_resolution_clock::now();
  223. int time_I = std::chrono::duration_cast<std::chrono::nanoseconds>(finish_I - start_I).count();
  224.  
  225. //Milisekundy
  226. //Testujemy w zaleznosci od casu, liczymy czas
  227. // Testing Quick Sort
  228. /* unsigned long q = clock();
  229. //quickSort(quick, 0, (N - 1));
  230. QuickSortIteretive(quick, 0, (N - 1));
  231. int time_Q = clock() - q;
  232. //Testing Buble Sort
  233. unsigned long bub = clock();
  234. BubleSort(buble);
  235. int time_B = clock() - bub;
  236. // Testing Insert Sort
  237. unsigned long ins = clock();
  238. InsertionSort(insert);
  239. int time_I = clock() - ins;
  240. */
  241. /* std::cout <<
  242. "\t Quick : " << time_Q <<
  243. "\t Buble : " << time_B <<
  244. "\t Insert : " << time_I <<
  245. std::endl;
  246. */
  247.  
  248. //--------------------------------------------------
  249. outQuick << time_Q << std::endl;
  250. //Buble
  251. outBuble << time_B << std::endl;
  252. //Insert
  253. outInsert << time_I << std::endl;
  254. //---------------------------------------------------
  255. delete[] quick;
  256. delete[] buble;
  257. delete[] insert;
  258. delete[] rezult;
  259. }
  260. outQuick.close();
  261. outBuble.close();
  262. outInsert.close();
  263. }
  264.  
  265.  
  266. int main()
  267. {
  268.  
  269. int* arr = new int[N];
  270. int* brr = new int[N]; // tablica na kolejne permutacje
  271. //Tworzymy tablice od 0 do n
  272. for (int i = 0; i < N; i++) {
  273. arr[i] = i;
  274. brr[i] = -1;
  275. }
  276. out.open("Permutations_10.csv", std::ios::app);
  277. Permutations(arr, brr, N);
  278. std::cout << "Generator wszystkich permutacj o dlugosci " << N << " zakonczyl dzialanie " << std::endl;
  279. out.close();
  280. in.open("Permutations_10.csv", std::ios::in | std::ios::out);
  281. Testing_3_functions();
  282. in.close();
  283.  
  284. std::fstream outQuick;
  285. outQuick.open("Quicksort_Permutations.csv", std::ios::in | std::ios::out);
  286.  
  287.  
  288. std::fstream outBuble;
  289. outBuble.open("Bublesort_Permutations.csv", std::ios::in | std::ios::out);
  290.  
  291.  
  292. std::fstream outInsert;
  293. outInsert.open("Insertsort_Permutations.csv", std::ios::in | std::ios::out);
  294.  
  295. int end = 1;
  296. for (int i = 1; i <= N; i++) {
  297. end *= i;
  298. }
  299. long unsigned int max_Q = 0;
  300. int min_Q = 10000000;
  301. long unsigned int suma_Q = 0;
  302. long unsigned int max_B = 0;
  303. int min_B = 10000000;
  304. long unsigned int suma_B = 0;
  305. long unsigned int max_I = 0;
  306. int min_I = 10000000;
  307. long unsigned int suma_I = 0;
  308. int min_id_Q = 0;
  309. int max_id_Q = 0;
  310. int min_id_B = 0;
  311. int max_id_B = 0;
  312. int min_id_I = 0;
  313. int max_id_I = 0;
  314.  
  315. for (int i = 0; i < end; i++) {
  316. int quick;
  317. outQuick >> quick;
  318. outQuick << std::endl;
  319. if (max_Q < quick) {
  320. max_Q = quick;
  321. max_id_Q = i;
  322. }
  323. if (min_Q > quick) {
  324. min_Q = quick;
  325. min_id_Q = i;
  326. }
  327. suma_Q += quick;
  328. int buble;
  329. outBuble >> buble;
  330. outBuble << std::endl;
  331. if (max_B < buble) {
  332. max_B = buble;
  333. max_id_B = i;
  334. }
  335. if (min_B > buble) {
  336. min_B = buble;
  337. min_id_B = i;
  338. }
  339. suma_B += buble;
  340. int insert;
  341. outInsert >> insert;
  342. outInsert << std::endl;
  343. if (max_I < insert) {
  344. max_I = insert;
  345. max_id_I = i;
  346. }
  347. if (min_I > insert) {
  348. min_I = insert;
  349. min_id_I = i;
  350. }
  351. suma_I += insert;
  352. // std::cout << quick << "\t" << buble << "\t" << insert << std::endl;
  353. if (outQuick.eof() || outBuble.eof() || outInsert.eof() ){
  354. end = i;
  355. break;
  356. }
  357. }
  358. std::fstream rezult;
  359. rezult.open("Rezult.txt", std::ios::app);
  360.  
  361. rezult << "QuickSort" << std::endl;
  362. rezult << "Czas pesymistyczny : " << max_Q << ".Ta tablica pod numerem : " <<max_id_Q + 1 << std::endl;
  363. rezult << "Czas optymistyczny : " << min_Q << ".Ta tablica pod numerem : " << min_id_Q + 1 << std::endl;
  364. rezult << "Czas sredni : " << (suma_Q / end) << std::endl;
  365.  
  366. rezult << "BubleSort" << std::endl;
  367. rezult << "Czas pesymistyczny : " << max_B << ".Ta tablica pod numerem : " << max_id_B + 1 << std::endl;
  368. rezult << "Czas optymistyczny : " << min_B << ".Ta tablica pod numerem : " << min_id_B + 1 << std::endl;
  369. rezult << "Czas sredni : " << (suma_B / end) << std::endl;
  370.  
  371. rezult << "InsertSort" << std::endl;
  372. rezult << "Czas pesymistyczny : " << max_I << ".Ta tablica pod numerem : " << max_id_I + 1 << std::endl;
  373. rezult << "Czas optymistyczny : " << min_I << ".Ta tablica pod numerem : " << min_id_I + 1 << std::endl;
  374. rezult << "Czas sredni : " << (suma_I / end) << std::endl;
  375.  
  376. rezult.close();
  377.  
  378. outQuick.close();
  379. outBuble.close();
  380. outInsert.close();
  381.  
  382.  
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement