Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- void sortowanie_babelkowe(int tab[], int n)
- {
- for (int i = 1; i<n; i++)
- {
- for (int j = n - 1; j >= 1; j--)
- {
- if (tab[j]<tab[j - 1])
- {
- int bufor;
- bufor = tab[j - 1];
- tab[j - 1] = tab[j];
- tab[j] = bufor;
- }
- }
- }
- }
- void quicksort(int tablica[], int lewy, int prawy)
- {
- int v = tablica[(lewy + prawy) / 2];
- int i, j, x;
- i = lewy;
- j = prawy;
- do
- {
- while (tablica[i]<v) i++;
- while (tablica[j]>v) j--;
- if (i <= j)
- {
- x = tablica[i];
- tablica[i] = tablica[j];
- tablica[j] = x;
- i++;
- j--;
- }
- } while (i <= j);
- if (j>lewy) quicksort(tablica, lewy, j);
- if (i<prawy) quicksort(tablica, i, prawy);
- }
- int main()
- {
- srand(time(NULL));
- const int ile = 100000;
- clock_t start, stop;
- double czas;
- int tablica[ile];
- int tablica2[ile];
- for (int i = 0; i<ile; i++)
- {
- tablica[i] = rand() % 100000 + 1;
- }
- for (int i = 0; i<ile; i++)
- {
- tablica2[i] = tablica[i];
- }
- cout << "Sortuje teraz babelkowo" << endl;
- start = clock();
- sortowanie_babelkowe(tablica, ile);
- stop = clock();
- czas = (double)(stop - start) / CLOCKS_PER_SEC;
- cout << endl << "Czas sortowania babelkowego: " << czas << " s" << endl;
- cout << endl << "Sortuje teraz algorytmem quicksort" << endl;
- start = clock();
- quicksort(tablica2, 0, ile - 1);
- stop = clock();
- czas = (double)(stop - start) / CLOCKS_PER_SEC;
- cout << endl << "Czas sortowania quicksort: " << czas << " s" << endl;
- int x;
- cin >> x;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement