Advertisement
kiubia

DOBRE_QUICK_SORT_ITERACYJNIE

Apr 2nd, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. // QUICK_SORT_ITERACYJNIE
  2.  
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <cstdlib>
  7. #include <time.h>
  8.  
  9. using namespace std;
  10.  
  11. const int N = 70000; // Liczebność zbioru.
  12.  
  13. int d[N];
  14.  
  15. // Procedura sortowania szybkiego
  16. //-------------------------------
  17.  
  18. void Sortuj_szybko(int lewy, int prawy)
  19. {
  20. int i,j,piwot;
  21.  
  22. i = (lewy + prawy) / 2;//PIWOT
  23. piwot = d[i]; d[i] = d[prawy];
  24. for(j = i = lewy; i < prawy; i++)
  25. if(d[i] < piwot)
  26. {
  27. swap(d[i], d[j]);
  28. j++;
  29. }
  30. d[prawy] = d[j]; d[j] = piwot;
  31. if(lewy < j - 1) Sortuj_szybko(lewy, j - 1);
  32. if(j + 1 < prawy) Sortuj_szybko(j + 1, prawy);
  33. }
  34.  
  35. // Program główny
  36. //---------------
  37. clock_t start, stop;
  38. double czas;
  39. int main()
  40. {
  41. int i;
  42. int k= N/2;
  43. srand((unsigned)time(NULL));
  44.  
  45. cout << " TABLICA:\n";
  46.  
  47. // Najpierw wypełniamy tablicę d[] liczbami pseudolosowymi
  48. // a następnie wyświetlamy jej zawartość
  49.  
  50. for(i = 0; i <= k; i++) d[i] = i;
  51. for(i=k; i<=N;i++) d[i]= k--;
  52. for(i = 0; i < N; i++) cout << setw(4) << d[i];
  53. cout << endl;
  54.  
  55. // Sortujemy
  56. start = clock();
  57. Sortuj_szybko(0,N - 1);
  58.  
  59. // Wyświetlamy wynik sortowania
  60. stop = clock();
  61. czas= (double) (stop-start)/CLOCKS_PER_SEC;
  62. cout << "Po sortowaniu:\n\n";
  63. for(i = 0; i < N; i++) cout << setw(4) << d[i];
  64. cout << endl;
  65. printf("Czas QS: %f\n",czas);
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement