Advertisement
AdrianMadajewski

Sortowanie bąbelkowe - Implementacja C++

Oct 19th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime> // time
  3. #include <cstdlib> // rand
  4.  
  5. using namespace std;
  6.  
  7. void bubbleSort(int *tab, int n)
  8. {
  9.     for(int i = 0; i < n - 1; i++)
  10.     // zakres n - 1 - i oznacza zawezenie indeksu
  11.     // rownoznacznie z postawieniem "flagi" na elementach
  12.     // juz posortowanych
  13.         for(int j = 0; j < n - 1 - i; j++)
  14.             if(tab[j] > tab[j + 1])
  15.                 swap(tab[j], tab[j + 1]);
  16. }
  17.  
  18. int main()
  19. {
  20.     const int N = 10;
  21.     int tab[N];
  22.  
  23.     // losowanie elementow tablicy
  24.     srand(time(NULL));
  25.     for(int i = 0; i < 10; i++)
  26.         tab[i] = rand() % 6;
  27.  
  28.     // wypisanie elementow przed
  29.     for(int a : tab)
  30.         cout << a << " ";
  31.  
  32.     // wypisanie elementow po sortowaniu
  33.     cout << endl << "POSORTOWANE\n";
  34.  
  35.     bubbleSort(tab, N);
  36.  
  37.     for(int a: tab)
  38.         cout << a << " ";
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement