Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime> // time
- #include <cstdlib> // rand
- using namespace std;
- void bubbleSort(int *tab, int n)
- {
- for(int i = 0; i < n - 1; i++)
- // zakres n - 1 - i oznacza zawezenie indeksu
- // rownoznacznie z postawieniem "flagi" na elementach
- // juz posortowanych
- for(int j = 0; j < n - 1 - i; j++)
- if(tab[j] > tab[j + 1])
- swap(tab[j], tab[j + 1]);
- }
- int main()
- {
- const int N = 10;
- int tab[N];
- // losowanie elementow tablicy
- srand(time(NULL));
- for(int i = 0; i < 10; i++)
- tab[i] = rand() % 6;
- // wypisanie elementow przed
- for(int a : tab)
- cout << a << " ";
- // wypisanie elementow po sortowaniu
- cout << endl << "POSORTOWANE\n";
- bubbleSort(tab, N);
- for(int a: tab)
- cout << a << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement