Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdlib.h>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. void wyswietlanie(int n, int t[])
  9. {
  10. for(int i=0; i<n; i++)
  11. {
  12. cout<<t[i]<<" ";
  13. }
  14.  
  15. cout<<endl;
  16. }
  17. int maksimum(int n, int t[])
  18. {
  19. int max=0;
  20. for(int i=0; i<n; i++)
  21. {
  22. if(t[i]>max)
  23. {
  24. max=t[i];
  25. }
  26. }
  27. return max;
  28. }
  29. int Max_element_indeks(int n, int t[])
  30. {
  31. int max = 0;
  32. for (int i = 1; i < n; i++)
  33. if (t[i] > t[max])
  34. max = i;
  35. return max;
  36. }
  37.  
  38. void Selection_sort(int n, int t[])
  39. {
  40. for (int i = n; i >= 2; i--)
  41. {
  42. int max = Max_element_indeks(i, t);
  43. if (max != i - 1)
  44. swap(t[i - 1], t[max]);
  45. }
  46.  
  47. cout<<"Select_sort: ";
  48. }
  49.  
  50. void Insert_sort(int n, int t[])
  51. {
  52. for(int i=1; i<n; i++)
  53. {
  54. int j=i;
  55. int temp=t[j];
  56.  
  57. while((j>0) && t[j-1]>temp)
  58. {
  59. t[j]=t[j-1];
  60. j--;
  61.  
  62. }
  63. t[j]=temp;
  64. }
  65.  
  66. cout<<"Insert_sort: ";
  67.  
  68. }
  69.  
  70. void bubblesort(int n, int t[])
  71. {
  72.  
  73. for(int i=0; i<n-1;i++)
  74. {
  75. for(int j=0; j<n-1-i; j++)
  76. {
  77. if(t[j+1] < t[j])
  78. {
  79. swap(t[j+1], t[j]);
  80.  
  81. }
  82. }
  83. }
  84.  
  85. cout<<"bubble_sort: ";
  86. }
  87.  
  88. int quick_sort(int t[], int lo, int hi)
  89. {
  90. int i=lo;
  91. int j=hi;
  92.  
  93. int x=t[(lo+hi)/2];
  94. do
  95. {
  96. while(t[i]<x) i++;
  97. while(t[j]>x) j--;
  98. if(i<=j)
  99. {
  100. swap(t[i], t[j]);
  101. i++;
  102. j--;
  103. }
  104.  
  105. }while(i<=j);
  106.  
  107. if(lo<j) quick_sort(t, lo, j);
  108. if(hi>i) quick_sort(t, i, hi);
  109. }
  110.  
  111. void count_sort(int T[], int n, int maksimum){
  112. int k=maksimum;
  113. int *Tp;
  114. Tp=new int [n];
  115.  
  116. int *TPom;
  117. TPom=new int[k+1];
  118.  
  119. int i;
  120.  
  121. for(i = 0 ; i < k+1 ; ++i)
  122. TPom[i] = 0;
  123.  
  124. for(i = 0 ; i < n ; ++i)
  125. ++TPom[T[i]];
  126.  
  127. for(i = 1 ; i < k+1 ; ++i)
  128. TPom[i] += TPom[i-1];
  129.  
  130. for(i = n-1 ; i >= 0 ; --i)
  131. Tp[--TPom[T[i]]] = T[i] ;
  132.  
  133.  
  134. for(i=0; i<n; i++)
  135. {
  136. T[i]=Tp[i];
  137. }
  138.  
  139. delete[] Tp;
  140. delete[] TPom;
  141. }
  142.  
  143. void merge(int a[], const int low, const int mid, const int high)
  144. {
  145.  
  146. int * b = new int[high+1-low];
  147. int h,i,j,k;
  148. h=low;
  149. i=0;
  150. j=mid+1;
  151.  
  152. while((h<=mid)&&(j<=high))
  153. {
  154. if(a[h]<=a[j])
  155. {
  156. b[i]=a[h];
  157. h++;
  158. }
  159. else
  160. {
  161. b[i]=a[j];
  162. j++;
  163. }
  164. i++;
  165. }
  166.  
  167. if(h>mid)
  168. {
  169. for(k=j;k<=high;k++)
  170. {
  171. b[i]=a[k];
  172. i++;
  173. }
  174. }
  175. else
  176. {
  177. for(k=h;k<=mid;k++)
  178. {
  179. b[i]=a[k];
  180. i++;
  181. }
  182. }
  183.  
  184. for(k=0;k<=high-low;k++)
  185. {
  186. a[k+low]=b[k];
  187. }
  188. delete[] b;
  189. }
  190.  
  191. void merge_sort(int a[], const int low, const int high)
  192. {
  193. int mid;
  194. if(low<high)
  195. {
  196. mid=(low+high)/2;
  197. merge_sort(a, low,mid);
  198. merge_sort(a, mid+1,high);
  199. merge(a, low,mid,high);
  200. }
  201. }
  202.  
  203. int rosnaco(int n, int t[])
  204. {
  205. for(int i=0; i<n; i++)
  206. {
  207. t[i]=i;
  208. }
  209. }
  210.  
  211. int malejaco(int n, int t[])
  212. {
  213. for(int i=n; i>0; i--)
  214. {
  215. t[n-i]=i;
  216. }
  217. }
  218.  
  219. void losowo(int n, int t[])
  220. {
  221. srand((unsigned) time(NULL));
  222.  
  223. for(int i=0; i<n; i++)
  224. {
  225. int liczba = rand() % 100 + 1;
  226. t[i]= liczba;
  227. }
  228. }
  229.  
  230.  
  231. int main()
  232. {
  233. for(int i=1; i<8; i++)
  234. {
  235. int stopien=pow(10,i);
  236. int *tab = new int [stopien];
  237. clock_t start, stop;
  238. double czas;
  239. if(i==1)
  240. {
  241.  
  242. cout<<"rosnaco"<<endl;
  243. rosnaco(stopien, tab);
  244. wyswietlanie(stopien, tab);
  245. start = clock();
  246. Selection_sort(stopien, tab);
  247. stop = clock();
  248. wyswietlanie(stopien, tab);
  249. czas = (stop - start);
  250. cout<<"czas w tikach procesora: "<<czas<<endl;
  251.  
  252. cout<<"losowo: "<<endl;
  253. losowo(stopien, tab);
  254. wyswietlanie(stopien, tab);
  255. start = clock();
  256. Selection_sort(stopien, tab);
  257. stop = clock();
  258. wyswietlanie(stopien, tab);
  259. czas = (stop - start);
  260. cout<<"czas w tikach procesora: "<<czas<<endl;
  261.  
  262. cout<<"malejaco"<<endl;
  263. malejaco(stopien, tab);
  264. wyswietlanie(stopien, tab);
  265. start=clock();
  266. Selection_sort(stopien, tab);
  267. stop =clock();
  268. wyswietlanie(stopien, tab);
  269. czas = (stop - start);
  270. cout<<"czas w tikach procesora: "<<czas<<endl;
  271.  
  272. cout<<endl;
  273. cout<<"rosnaco"<<endl;
  274. rosnaco(stopien, tab);
  275. wyswietlanie(stopien, tab);
  276. start = clock();
  277. Insert_sort(stopien, tab);
  278. stop = clock();
  279. wyswietlanie(stopien, tab);
  280. czas = (stop - start);
  281. cout<<"czas w tikach procesora: "<<czas<<endl;
  282.  
  283. cout<<"losowo"<<endl;
  284. losowo(stopien, tab);
  285. wyswietlanie(stopien, tab);
  286. start = clock();
  287. Insert_sort(stopien, tab);
  288. stop = clock();
  289. wyswietlanie(stopien, tab);
  290. czas = (stop - start);
  291. cout<<"czas w tikach procesora: "<<czas<<endl;
  292.  
  293. cout<<"malejaco"<<endl;
  294. malejaco(stopien, tab);
  295. wyswietlanie(stopien, tab);
  296. start = clock();
  297. Insert_sort(stopien, tab);
  298. stop = clock();
  299. wyswietlanie(stopien, tab);
  300. czas = (stop - start);
  301. cout<<"czas w tikach procesora: "<<czas<<endl;
  302.  
  303.  
  304. cout<<endl;
  305. cout<<"rosnaco"<<endl;
  306. rosnaco(stopien, tab);
  307. wyswietlanie(stopien, tab);
  308. start = clock();
  309. bubblesort(stopien, tab);
  310. stop = clock();
  311. wyswietlanie(stopien, tab);
  312. czas = (stop - start);
  313. cout<<"czas w tikach procesora: "<<czas<<endl;
  314.  
  315. cout<<"losowo"<<endl;
  316. losowo(stopien, tab);
  317. wyswietlanie(stopien, tab);
  318. start = clock();
  319. bubblesort(stopien, tab);
  320. stop = clock();
  321. wyswietlanie(stopien, tab);
  322. czas = (stop - start);
  323. cout<<"czas w tikach procesora: "<<czas<<endl;
  324.  
  325. cout<<"malejaco"<<endl;
  326. malejaco(stopien, tab);
  327. wyswietlanie(stopien, tab);
  328. start = clock();
  329. bubblesort(stopien, tab);
  330. stop = clock();
  331. wyswietlanie(stopien, tab);
  332. czas = (stop - start);
  333. cout<<"czas w tikach procesora: "<<czas<<endl;
  334.  
  335. cout<<endl;
  336. cout<<"rosnaco"<<endl;
  337. rosnaco(stopien, tab);
  338. wyswietlanie(stopien, tab);
  339. start = clock();
  340. quick_sort(tab, 0, stopien);
  341. stop = clock();
  342. cout<<"quick_sort: "<<endl;
  343. wyswietlanie(stopien, tab);
  344. czas = (stop - start);
  345. cout<<"czas w tikach procesora: "<<czas<<endl;
  346.  
  347. cout<<"losowo"<<endl;
  348. losowo(stopien, tab);
  349. wyswietlanie(stopien, tab);
  350. start = clock();
  351. quick_sort(tab, 0, stopien);
  352. stop = clock();
  353. cout<<"quick_sort: "<<endl;
  354. wyswietlanie(stopien, tab);
  355. czas = (stop - start);
  356. cout<<"czas w tikach procesora: "<<czas<<endl;
  357.  
  358. cout<<"malejaco"<<endl;
  359. malejaco(stopien, tab);
  360. wyswietlanie(stopien, tab);
  361. start = clock();
  362. quick_sort(tab, 0, stopien);
  363. stop = clock();
  364. cout<<"quick_sort: "<<endl;
  365. wyswietlanie(stopien, tab);
  366. czas = (stop - start);
  367. cout<<"czas w tikach procesora: "<<czas<<endl;
  368.  
  369. cout<<endl;
  370. cout<<"rosnaco"<<endl;
  371. rosnaco(stopien, tab);
  372. wyswietlanie(stopien, tab);
  373. start = clock();
  374. count_sort(tab, stopien, maksimum(stopien, tab));
  375. stop = clock();
  376. cout<<"count_sort: "<<endl;
  377. wyswietlanie(stopien, tab);
  378. czas = (stop - start);
  379. cout<<"czas w tikach procesora: "<<czas<<endl;
  380.  
  381. cout<<"losowo"<<endl;
  382. losowo(stopien, tab);
  383. wyswietlanie(stopien, tab);
  384. start = clock();
  385. count_sort(tab, stopien, maksimum(stopien, tab));
  386. stop = clock();
  387. cout<<"count_sort: "<<endl;
  388. wyswietlanie(stopien, tab);
  389. czas = (stop - start);
  390. cout<<"czas w tikach procesora: "<<czas<<endl;
  391.  
  392. cout<<"malejaco"<<endl;
  393. malejaco(stopien, tab);
  394. wyswietlanie(stopien, tab);
  395. start = clock();
  396. count_sort(tab, stopien, maksimum(stopien, tab));
  397. stop = clock();
  398. cout<<"count_sort: "<<endl;
  399. wyswietlanie(stopien, tab);
  400. czas = (stop - start);
  401. cout<<"czas w tikach procesora: "<<czas<<endl;
  402.  
  403. cout<<endl;
  404. cout<<"rosnaco"<<endl;
  405. rosnaco(stopien, tab);
  406. wyswietlanie(stopien, tab);
  407. start = clock();
  408. merge_sort(tab,0, stopien);
  409. stop = clock();
  410. cout<<"merge_sort: "<<endl;
  411. wyswietlanie(stopien, tab);
  412. czas = (stop - start);
  413. cout<<"czas w tikach procesora: "<<czas<<endl;
  414.  
  415. cout<<"losowo"<<endl;
  416. losowo(stopien, tab);
  417. wyswietlanie(stopien, tab);
  418. start = clock();
  419. merge_sort(tab,0, stopien);
  420. stop = clock();
  421. cout<<"merge_sort: "<<endl;
  422. wyswietlanie(stopien, tab);
  423. czas = (stop - start);
  424. cout<<"czas w tikach procesora: "<<czas<<endl;
  425.  
  426. cout<<"malejaco"<<endl;
  427. malejaco(stopien, tab);
  428. wyswietlanie(stopien, tab);
  429. start = clock();
  430. merge_sort(tab,0, stopien);
  431. stop = clock();
  432. cout<<"merge_sort: "<<endl;
  433. wyswietlanie(stopien, tab);
  434. czas = (stop - start);
  435. cout<<"czas w tikach procesora: "<<czas<<endl;
  436. }
  437.  
  438. else
  439.  
  440. {
  441.  
  442. if(i<5)
  443. {
  444. cout<<endl;
  445. cout<<"10^"<<i<<endl;
  446. cout<<"rosnaco"<<endl;
  447. rosnaco(stopien, tab);
  448. start = clock();
  449. Selection_sort(stopien, tab);
  450. stop = clock();
  451. czas = (stop - start);
  452. cout<<"czas w tikach procesora: "<<czas<<endl;
  453.  
  454. cout<<"losowo"<<endl;
  455. losowo(stopien, tab);
  456. start = clock();
  457. Selection_sort(stopien, tab);
  458. stop = clock();
  459. czas = (stop - start);
  460. cout<<"czas w tikach procesora: "<<czas<<endl;
  461.  
  462. cout<<"malejaco"<<endl;
  463. malejaco(stopien, tab);
  464. start=clock();
  465. Selection_sort(stopien, tab);
  466. stop =clock();
  467. czas = (stop - start);
  468. cout<<"czas w tikach procesora: "<<czas<<endl;
  469.  
  470. cout<<endl;
  471. cout<<"10^"<<i<<endl;
  472. cout<<"rosnaco"<<endl;
  473. rosnaco(stopien, tab);
  474. start = clock();
  475. Insert_sort(stopien, tab);
  476. stop = clock();
  477. czas = (stop - start);
  478. cout<<"czas w tikach procesora: "<<czas<<endl;
  479.  
  480. cout<<"losowo"<<endl;
  481. losowo(stopien, tab);
  482. start = clock();
  483. Insert_sort(stopien, tab);
  484. stop = clock();
  485. czas = (stop - start);
  486. cout<<"czas w tikach procesora: "<<czas<<endl;
  487.  
  488. cout<<"malejaco"<<endl;
  489. malejaco(stopien, tab);
  490. start = clock();
  491. Insert_sort(stopien, tab);
  492. stop = clock();
  493. czas = (stop - start);
  494. cout<<"czas w tikach procesora: "<<czas<<endl;
  495.  
  496.  
  497. cout<<endl;
  498. cout<<"10^"<<i<<endl;
  499. cout<<"rosnaco"<<endl;
  500. rosnaco(stopien, tab);
  501. start = clock();
  502. bubblesort(stopien, tab);
  503. stop = clock();
  504. czas = (stop - start);
  505. cout<<"czas w tikach procesora: "<<czas<<endl;
  506.  
  507. cout<<"losowo"<<endl;
  508. losowo(stopien, tab);
  509. start = clock();
  510. bubblesort(stopien, tab);
  511. stop = clock();
  512. czas = (stop - start);
  513. cout<<"czas w tikach procesora: "<<czas<<endl;
  514.  
  515. cout<<"malejaco"<<endl;
  516. malejaco(stopien, tab);
  517. start = clock();
  518. bubblesort(stopien, tab);
  519. stop = clock();
  520. czas = (stop - start);
  521. cout<<"czas w tikach procesora: "<<czas<<endl;
  522.  
  523. cout<<endl;
  524. cout<<"10^"<<i<<endl;
  525. cout<<"rosnaco"<<endl;
  526. rosnaco(stopien, tab);
  527. start = clock();
  528. quick_sort(tab, 0, stopien);
  529. stop = clock();
  530. cout<<"quick_sort: "<<endl;
  531. czas = (stop - start);
  532. cout<<"czas w tikach procesora: "<<czas<<endl;
  533.  
  534. cout<<"losowo"<<endl;
  535. losowo(stopien, tab);
  536. start = clock();
  537. quick_sort(tab, 0, stopien);
  538. stop = clock();
  539. cout<<"quick_sort: "<<endl;
  540. czas = (stop - start);
  541. cout<<"czas w tikach procesora: "<<czas<<endl;
  542.  
  543. cout<<"malejaco"<<endl;
  544. malejaco(stopien, tab);
  545. start = clock();
  546. quick_sort(tab, 0, stopien);
  547. stop = clock();
  548. cout<<"quick_sort: "<<endl;
  549. czas = (stop - start);
  550. cout<<"czas w tikach procesora: "<<czas<<endl;
  551.  
  552. cout<<endl;
  553. cout<<"10^"<<i<<endl;
  554. cout<<"rosnaco"<<endl;
  555. rosnaco(stopien, tab);
  556. start = clock();
  557. count_sort(tab, stopien, maksimum(stopien, tab));
  558. stop = clock();
  559. cout<<"count_sort: "<<endl;
  560. czas = (stop - start);
  561. cout<<"czas w tikach procesora: "<<czas<<endl;
  562.  
  563. cout<<"losowo"<<endl;
  564. losowo(stopien, tab);
  565. start = clock();
  566. count_sort(tab, stopien, maksimum(stopien, tab));
  567. stop = clock();
  568. cout<<"count_sort: "<<endl;
  569. czas = (stop - start);
  570. cout<<"czas w tikach procesora: "<<czas<<endl;
  571.  
  572. cout<<"malejaco"<<endl;
  573. malejaco(stopien, tab);
  574. start = clock();
  575. count_sort(tab, stopien, maksimum(stopien, tab));
  576. stop = clock();
  577. cout<<"count_sort: "<<endl;
  578. czas = (stop - start);
  579. cout<<"czas w tikach procesora: "<<czas<<endl;
  580.  
  581. cout<<endl;
  582. cout<<"10^"<<i<<endl;
  583. cout<<"rosnaco"<<endl;
  584. rosnaco(stopien, tab);
  585. start = clock();
  586. merge_sort(tab,0, stopien);
  587. stop = clock();
  588. cout<<"merge_sort: "<<endl;
  589. czas = (stop - start);
  590. cout<<"czas w tikach procesora: "<<czas<<endl;
  591.  
  592. cout<<"losowo"<<endl;
  593. losowo(stopien, tab);
  594. start = clock();
  595. merge_sort(tab,0, stopien);
  596. stop = clock();
  597. cout<<"merge_sort: "<<endl;
  598. czas = (stop - start);
  599. cout<<"czas w tikach procesora: "<<czas<<endl;
  600.  
  601. cout<<"malejaco"<<endl;
  602. malejaco(stopien, tab);
  603. start = clock();
  604. merge_sort(tab, 0, stopien);
  605. stop = clock();
  606. cout<<"merge_sort: "<<endl;
  607. czas = (stop - start);
  608. cout<<"czas w tikach procesora: "<<czas<<endl;
  609.  
  610. }
  611. else
  612. {
  613. cout<<endl;
  614. cout<<"10^"<<i<<endl;
  615. cout<<"rosnaco"<<endl;
  616. rosnaco(stopien, tab);
  617. start = clock();
  618. quick_sort(tab, 0, stopien);
  619. stop = clock();
  620. cout<<"quick_sort: "<<endl;
  621. czas = (stop - start);
  622. cout<<"czas w tikach procesora: "<<czas<<endl;
  623.  
  624. cout<<"losowo"<<endl;
  625. losowo(stopien, tab);
  626. start = clock();
  627. quick_sort(tab, 0, stopien);
  628. stop = clock();
  629. cout<<"quick_sort: "<<endl;
  630. czas = (stop - start);
  631. cout<<"czas w tikach procesora: "<<czas<<endl;
  632.  
  633. cout<<"malejaco"<<endl;
  634. malejaco(stopien, tab);
  635. start = clock();
  636. quick_sort(tab, 0, stopien);
  637. stop = clock();
  638. cout<<"quick_sort: "<<endl;
  639. czas = (stop - start);
  640. cout<<"czas w tikach procesora: "<<czas<<endl;
  641.  
  642. cout<<endl;
  643. cout<<"10^"<<i<<endl;
  644. cout<<"rosnaco"<<endl;
  645. rosnaco(stopien, tab);
  646. start = clock();
  647. count_sort(tab, stopien, maksimum(stopien, tab));
  648. stop = clock();
  649. cout<<"count_sort: "<<endl;
  650. czas = (stop - start);
  651. cout<<"czas w tikach procesora: "<<czas<<endl;
  652.  
  653. cout<<"losowo"<<endl;
  654. losowo(stopien, tab);
  655. start = clock();
  656. count_sort(tab, stopien, maksimum(stopien, tab));
  657. stop = clock();
  658. cout<<"count_sort: "<<endl;
  659. czas = (stop - start);
  660. cout<<"czas w tikach procesora: "<<czas<<endl;
  661.  
  662. cout<<"malejaco"<<endl;
  663. malejaco(stopien, tab);
  664. start = clock();
  665. count_sort(tab, stopien, maksimum(stopien, tab));
  666. stop = clock();
  667. cout<<"count_sort: "<<endl;
  668. czas = (stop - start);
  669. cout<<"czas w tikach procesora: "<<czas<<endl;
  670.  
  671. cout<<endl;
  672. cout<<"10^"<<i<<endl;
  673. cout<<"rosnaco"<<endl;
  674. rosnaco(stopien, tab);
  675. start = clock();
  676. merge_sort(tab,0, stopien);
  677. stop = clock();
  678. cout<<"merge_sort: "<<endl;
  679. czas = (stop - start);
  680. cout<<"czas w tikach procesora: "<<czas<<endl;
  681.  
  682. cout<<"losowo"<<endl;
  683. losowo(stopien, tab);
  684. start = clock();
  685. merge_sort(tab,0, stopien);
  686. stop = clock();
  687. cout<<"merge_sort: "<<endl;
  688. czas = (stop - start);
  689. cout<<"czas w tikach procesora: "<<czas<<endl;
  690.  
  691. cout<<"malejaco"<<endl;
  692. malejaco(stopien, tab);
  693. start = clock();
  694. merge_sort(tab, 0, stopien);
  695. stop = clock();
  696. cout<<"merge_sort: "<<endl;
  697. czas = (stop - start);
  698. cout<<"czas w tikach procesora: "<<czas<<endl;
  699.  
  700. }
  701. }
  702. delete[] tab;
  703. }
  704. return 0;
  705. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement