Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // QUICK_SORT_ITERACYJNIE
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <time.h>
- using namespace std;
- const int N = 70000; // Liczebność zbioru.
- int d[N];
- // Procedura sortowania szybkiego
- //-------------------------------
- void Sortuj_szybko(int lewy, int prawy)
- {
- int i,j,piwot;
- i = (lewy + prawy) / 2;//PIWOT
- piwot = d[i]; d[i] = d[prawy];
- for(j = i = lewy; i < prawy; i++)
- if(d[i] < piwot)
- {
- swap(d[i], d[j]);
- j++;
- }
- d[prawy] = d[j]; d[j] = piwot;
- if(lewy < j - 1) Sortuj_szybko(lewy, j - 1);
- if(j + 1 < prawy) Sortuj_szybko(j + 1, prawy);
- }
- // Program główny
- //---------------
- clock_t start, stop;
- double czas;
- int main()
- {
- int i;
- int k= N/2;
- srand((unsigned)time(NULL));
- cout << " TABLICA:\n";
- // Najpierw wypełniamy tablicę d[] liczbami pseudolosowymi
- // a następnie wyświetlamy jej zawartość
- for(i = 0; i <= k; i++) d[i] = i;
- for(i=k; i<=N;i++) d[i]= k--;
- for(i = 0; i < N; i++) cout << setw(4) << d[i];
- cout << endl;
- // Sortujemy
- start = clock();
- Sortuj_szybko(0,N - 1);
- // Wyświetlamy wynik sortowania
- stop = clock();
- czas= (double) (stop-start)/CLOCKS_PER_SEC;
- cout << "Po sortowaniu:\n\n";
- for(i = 0; i < N; i++) cout << setw(4) << d[i];
- cout << endl;
- printf("Czas QS: %f\n",czas);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement