Guest User

Untitled

a guest
Mar 22nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. void hs();
  7. void shs();
  8. void is(int [], int);
  9. void ss();
  10. void qs_prawy(int, int);
  11. void qs_los(int, int);
  12. void losuj_r();
  13. void losuj_m();
  14. void losuj_l();
  15. void losuj_s();
  16. void losuj_a();
  17. void wyswietl(int [], int);
  18. void powrot();
  19. void menu();
  20.  
  21. int *tab,*tabqs;
  22. int n = 0;
  23.  
  24. int main()
  25. {
  26.  
  27. cout << "podaj ilosc liczb do losowania" << endl;
  28. cin >> n;
  29. tab = new int[n];
  30. menu();
  31. getchar();
  32.  
  33. return 0;
  34. }
  35.  
  36. void wyswietl(int r[], int nn)
  37. {
  38. cout <<endl<< " przed:" << endl;
  39. for (int i = 0; i <= nn-1; i++)
  40. cout << tab[i] << " ";
  41. cout << endl<<" po:" << endl;
  42. for (int i = 0; i <= nn-1; i++)
  43. cout << r[i] << " ";
  44.  
  45. }
  46.  
  47. void losuj_r()
  48. {
  49. srand(time(NULL));
  50. int i = 0, gg;
  51. bool jest = true;//tablica rosnaca
  52. while (jest)
  53. {
  54. tab[i] = i;
  55. i++;
  56. if (i == n)
  57. jest = false;
  58.  
  59. }
  60. powrot();
  61. }
  62. void losuj_m()
  63. {
  64. srand(time(NULL));
  65. int i = n, gg;
  66. bool jest = true;//tablica malejaca
  67.  
  68.  
  69. while (jest)
  70. {
  71.  
  72.  
  73. tab[i] = i;
  74. i--;
  75. if (i == 0)
  76. jest = false;
  77.  
  78. }
  79. powrot();
  80. }
  81. void losuj_s()
  82. {
  83. srand(time(NULL));
  84. int i = 0, gg;
  85. bool jest = true;//tablica stala
  86.  
  87. gg = rand() % 100 + 1;
  88. while (jest)
  89. {
  90. tab[i] = gg;
  91. i++;
  92. if (i == n)
  93. jest = false;
  94.  
  95. }
  96. powrot();
  97. }
  98. void losuj_l()
  99. {
  100. srand(time(NULL));
  101. int i = 0, gg;
  102. bool jest = true;//tablica losowa
  103.  
  104. while (jest)
  105. {
  106. tab[i] = rand() % 100 + 1;
  107. i++;
  108. if (i == n)
  109. jest = false;
  110.  
  111. }
  112. powrot();
  113. }
  114. void losuj_a()
  115. {
  116. srand(time(NULL));
  117. int i = 0, gg;
  118. bool jest = true;//tablica a
  119.  
  120.  
  121.  
  122. while (jest)
  123. {
  124.  
  125. if ( i < n / 2)
  126. {
  127. tab[i] = i;
  128. i++;
  129. }
  130. if (i == n / 2)
  131. gg = i;
  132.  
  133. if (i >= n / 2)
  134. {
  135. tab[i] =gg;
  136. gg--;
  137. i++;
  138. }
  139.  
  140. if (i == n)
  141. jest = false;
  142.  
  143. }
  144. powrot();
  145. }
  146. void ss()
  147. {
  148. int m;
  149. clock_t at, bt;
  150. int *tabb = new int[n];
  151. for (int i = 0; i <= n; i++)
  152. tabb[i] = tab[i];
  153.  
  154. at = clock();
  155. for (int i = 0; i < n - 1; i++)
  156. {
  157. m = i;
  158. for (int j = i + 1; j < n; j++)
  159. {
  160. if (tabb[j] < tabb[m])
  161. m = j;
  162. swap(tabb[m], tabb[i]);
  163. }
  164. }
  165. bt = clock();
  166. // wyswietl(tabb, n);
  167. cout << endl << "czas:" << ((long float)(bt - at)) / CLOCKS_PER_SEC;
  168. delete tabb;
  169. powrot();
  170.  
  171. }
  172. void shs()
  173. {
  174. int h,temp,z;
  175. int *tabb = new int[n];
  176. clock_t at, bt;
  177.  
  178. for (int i = 1; i <= n; i++)
  179. tabb[i] = tab[i -1];
  180.  
  181. at = clock();
  182. for (h = 1; h < n; h = 3 * h + 1);//ten smieszny wspolczynnik
  183. h /= 9;
  184.  
  185. while (h)
  186. {
  187. for (int i = n - h - 1; i >= 0; i--)
  188. {
  189. temp = tabb[i];
  190. z = i + h;
  191. while ((z < n) && (temp > tabb[z]))
  192. {
  193. tabb[z - h] = tabb[z];
  194. z += h;
  195. }
  196. tabb[z - h] = temp;
  197. }
  198. h /= 3;
  199. }
  200. bt = clock();
  201. // cout << endl << " przed:" << endl;
  202. // for (int i = 0; i <= n - 1; i++)
  203. // cout << tab[i] << " ";
  204. // cout << endl << " po:" << endl;
  205. // for (int i = 1; i <= n; i++)
  206. // cout << tabb[i] << " ";
  207.  
  208. cout << endl << "czas:" << ((long float)(bt - at)) / CLOCKS_PER_SEC;
  209. delete tabb;
  210. powrot();
  211. }
  212. void is(int r[],int nn)//insertion sort sortowanie przez wstawianie
  213. {
  214. int j,temp;
  215. clock_t at,bt;
  216. int *tabb=new int[n];
  217.  
  218. for (int i = 0; i <= n; i++)
  219. tabb[i] = tab[i];
  220. at = clock();
  221. for (int i = 0; i < nn; i++)
  222. {
  223. temp = tabb[i];
  224. for (j = i - 1; j >= 0 && tabb[j]>temp; j--)
  225. tabb[j + 1] = tabb[j];
  226. tabb[j + 1] = temp;
  227.  
  228. }
  229. bt = clock();
  230.  
  231. //wyswietl(tabb, n);
  232. cout << endl << "czas:" << ((long float)(bt-at)) / CLOCKS_PER_SEC;
  233. getchar();
  234. delete tabb;
  235. powrot();
  236. }
  237. void hs()
  238. {
  239. int r, d, temp,zam;
  240.  
  241. int *tabb = new int[n];
  242. clock_t at, bt;
  243.  
  244. for (int i = 1; i <= n; i++)
  245. tabb[i] = tab[i-1];
  246.  
  247. at = clock();
  248. for (int i = n; i >=2; i--) //ukladanie kupki, od dolu
  249. {
  250. d = i;//dziecko nizej niz rodzic
  251. r = d / 2;//rodzic nad dzieckiem
  252. temp = tabb[i];
  253. while (temp > tabb[r] && r > 0) //tu srawdazamy czy to co mamy w tempie jest wieksze do tego co nad nim
  254. {
  255. swap(tabb[d], tabb[r]);
  256. d = r;
  257. r = d / 2; // te w linikjki przechodza w gore jesli tak w sensie do rodzica rodzica
  258. }
  259. }
  260.  
  261. cout << endl << " po zbudowaniu kupki:" << endl;
  262. for (int i = 1; i <= n; i++)
  263. cout << tabb[i] << " ";
  264.  
  265. for (int i = n; i >= 2; i--) //skladanie kupki,
  266. {
  267. swap(tabb[1], tabb[i]);
  268. r = 1;
  269. d = r + r;
  270. while (d < i)//bedzie dzilac dopoki dziecko bedzie mneijsze od i
  271. {
  272.  
  273. if (tabb[r] <= tabb[d + 1])//sprawdzamy czy prawa sciezka jest wieksza
  274. zam = d + 1;//jak tak to d wieksze o 1 i lecimy prawa sciezka
  275. else
  276. zam = d;//jak nie to zostawiamy i lecimy lewa
  277. swap(tabb[r], tabb[zam]);//zamiana atm gdzie poszlismy
  278. r = zam;//zwiekszamy rodzica
  279. d = 2 * r;//i dziecko
  280. }
  281. }
  282.  
  283. bt = clock();
  284.  
  285. cout << endl << " przed:" << endl;
  286. for (int i = 0; i <= n - 1; i++)
  287. cout << tab[i] << " ";
  288. cout << endl << " po:" << endl;
  289. for (int i = 1; i <= n; i++)
  290. cout << tabb[i] << " ";
  291. cout << endl << "czas:" << ((long float)(bt - at)) / CLOCKS_PER_SEC;
  292.  
  293. delete tabb;
  294. powrot();
  295. }
  296. void qs_prawy(int i, int j)
  297. {
  298. int lewy = i, prawy = j;
  299. /*int piwot=tabqs[prawy];
  300. int current = lewy;
  301.  
  302. for (current; current < prawy; current++)
  303. {
  304. if (tabqs[current] < piwot)
  305. {
  306. swap(tabqs[current], tabqs[lewy]);
  307. lewy++;
  308. }
  309. }
  310. swap(tabqs[lewy], tabqs[prawy]);
  311. qs_prawy(0, lewy);
  312. qs_prawy(lewy+1, prawy);*/
  313.  
  314.  
  315. }
  316. void qs_los(int i, int j)
  317. {
  318. srand(time(NULL));
  319. int lewy = i, prawy = j;
  320. int piwot = rand() % lewy + prawy;;
  321. int current = lewy;
  322.  
  323. for (current; current <= prawy; current++)//
  324. {
  325. if (tabqs[current] < tabqs[piwot])
  326. {
  327. swap(tabqs[current], tabqs[lewy]);
  328. lewy++;
  329. }
  330. if (tabqs[current] > tabqs[piwot])
  331. {
  332. swap(tabqs[current], tabqs[prawy]);
  333. prawy--;
  334. }
  335. }
  336.  
  337. qs_los(0, piwot);
  338. qs_los(piwot + 1, prawy);
  339.  
  340. }
  341.  
  342.  
  343.  
  344.  
  345. void powrot()
  346. {
  347.  
  348. cout <<endl<< "wcisnij enter by wrocic do menu" << endl;
  349. getchar();
  350. getchar();
  351. system("cls");
  352. menu();
  353. }
  354.  
  355.  
  356. void menu()
  357. {
  358. int menu = 0;
  359. system("cls");
  360.  
  361. cout << "1. ss" << endl;
  362. cout << "2. hs" << endl;
  363. cout << "3. is" << endl;
  364. cout << "4. shs" << endl;
  365. cout << "5. qs prawy" << endl;
  366. cout << "6. rosnaco" << endl;
  367. cout << "7. malejaco" << endl;
  368. cout << "8. stala" << endl;
  369. cout << "9. losowa" << endl;
  370. cout << "10. a-ksztaltna" << endl;
  371. cout << "11. qs losowy" << endl;
  372.  
  373. cin >> menu;
  374.  
  375. switch (menu)
  376. {
  377. case 1: ss(); break;
  378. case 2: hs(); break;
  379. case 3: is(tab,n); break;
  380. case 4: shs(); break;
  381. case 5:
  382. {
  383. tabqs = new int[n];
  384. clock_t at, bt;
  385.  
  386. for (int i = 0; i <= n; i++)
  387. tabqs[i] = tab[i];
  388. at = clock();
  389. qs_prawy(0, n - 1);
  390. bt = clock();
  391. wyswietl(tabqs, n);
  392. cout << endl << "czas:" << ((long float)(bt - at)) / CLOCKS_PER_SEC;
  393. powrot();
  394.  
  395. }; break;
  396. case 6: losuj_r(); break;
  397. case 7: losuj_m(); break;
  398. case 8: losuj_s(); break;
  399. case 9: losuj_l(); break;
  400. case 10: losuj_a(); break;
  401. case 11:
  402. {
  403. tabqs = new int[n];
  404. clock_t at, bt;
  405.  
  406. for (int i = 0; i <= n; i++)
  407. tabqs[i] = tab[i];
  408. at = clock();
  409. qs_prawy(0, n - 1);
  410. bt = clock();
  411. wyswietl(tabqs, n);
  412. cout << endl << "czas:" << ((long float)(bt - at)) / CLOCKS_PER_SEC;
  413. powrot();
  414.  
  415. }; break;
  416. }
  417.  
  418. }
Advertisement
Add Comment
Please, Sign In to add comment