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 selectionSort(int *tab, int n)
- {
- for(int i = 0; i < n - 1; i++)
- {
- int pmin = i; // pmin - pozycja minimalnej wartosci
- for(int j = i + 1; j < n; j++)
- if(tab[j] < tab[pmin]) pmin = j;
- swap(tab[i], tab[pmin]); // zamiana indeksow
- }
- }
- 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 (zmieniony syntax)
- for(int a : tab)
- cout << a << " ";
- // wypisanie elementow po sortowaniu
- cout << endl << "POSORTOWANE\n";
- selectionSort(tab, N);
- for(int a: tab)
- cout << a << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement