Advertisement
Marcel12311

Sortowanie bąbelkowe z użyciem klasy

Jan 11th, 2021 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. class MainA{
  5. private:
  6.     int ile;
  7.     int* tablica;
  8. public:
  9.     MainA(int=15);//konstruktor
  10.     ~MainA(){delete[] tablica;}//destruktor
  11.     void sortowanie_babelkowe(int[],int);// metoda sortowania babelkowego
  12.     int ZwrocIle(){return ile;} // metoda zwracajaca ile liczb do posortowania
  13.     int* ZwrocTablice(){return tablica;} // metoda zwraca cala tablice
  14. };
  15. MainA::MainA(int x){
  16.     ile=x;
  17.     tablica=new int[ile];
  18. }
  19. void MainA::sortowanie_babelkowe(int tab[],int n){
  20.     for(int i=0;i<n;i++){
  21.         for(int j=n-1;j>=1;j--){
  22.             if(tab[j]>tab[j-1]){
  23.                 int bufor;bufor=tab[j-1];
  24.                 tab[j-1]=tab[j];
  25.                 tab[j]=bufor;
  26.             }
  27.         }
  28.     }
  29. }
  30. int main()
  31. {
  32.     srand(time(NULL));
  33.     // stworzenie obiektu (obj1)
  34.     MainA obj1;
  35.  
  36.     // przepisanie losowych liczb do tablicy
  37.     for(int i=0;i<obj1.ZwrocIle();i++){
  38.         obj1.ZwrocTablice()[i]=rand()%100+1;
  39.     }
  40.  
  41.     // metoda ktora wywoluje sortowanie
  42.     obj1.sortowanie_babelkowe(obj1.ZwrocTablice(),obj1.ZwrocIle());
  43.  
  44.     // wyswietlenie tablicy
  45.     for(int i=0;i<obj1.ZwrocIle();i++){
  46.         cout << obj1.ZwrocTablice()[i]<<" ";
  47.     }
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement