Advertisement
Mikeellee

Sortowanie babelkowe C++

Dec 10th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. int ile;
  8. clock_t start,stop;
  9. double czas;
  10.  
  11. void sortowanie_babelkowe(int *t, int n)
  12. {
  13.     for(int i=1; i<n; i++)
  14.     {
  15.         for(int j=n-1; j>=1; j--)
  16.         {
  17.             if(t[j]<t[j-1])
  18.             {
  19.                 int bufor;
  20.                 bufor=t[j-1];
  21.                 t[j-1]=t[j];
  22.                 t[j]=bufor;
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32.     cout << "Porownanie czasow sortowania v.1" << endl;
  33.  
  34.     cout<<"Ile losowych liczb w tablicy: ";
  35.     cin>>ile;
  36.  
  37.     //dynamiczna alokacja tablicy
  38.     int *tab;
  39.     tab=new int [ile];
  40.  
  41.     srand(time(NULL));
  42.  
  43.     for(int i=0; i<ile; i++)
  44.     {
  45.         tab[i] = rand()%100+1;
  46.     }
  47.  
  48.  
  49.  
  50.  
  51.         cout<<"Przed posortowaniem: "<<endl;
  52.         for(int i=0; i<ile; i++)
  53.         {
  54.             cout<<tab[i]<<" ";
  55.         }
  56.  
  57.     cout<<endl<<"Sortuje teraz babelkowo. Prosze czekac!"<<endl;
  58.     start = clock();
  59.     sortowanie_babelkowe(tab,ile);
  60.     stop = clock();
  61.     czas = (double)(stop-start) / CLOCKS_PER_SEC;
  62.     cout<<endl<<"Czas sortowania babelkowego: "<<czas<<" s"<<endl;
  63.  
  64.     cout<<endl<<"Po posortowaniu: "<<endl;
  65.         for(int i=0; i<ile; i++)
  66.         {
  67.             cout<<tab[i]<<" ";
  68.         }
  69.  
  70.     delete [] tab;
  71.  
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement