Advertisement
AdrianMadajewski

Sortowanie przez wybór - Implementacja C++

Oct 19th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime> // time
  3. #include <cstdlib> // rand
  4.  
  5. using namespace std;
  6.  
  7. void selectionSort(int *tab, int n)
  8. {
  9.     for(int i = 0; i < n - 1; i++)
  10.     {
  11.         int pmin = i; // pmin - pozycja minimalnej wartosci
  12.         for(int j = i + 1; j < n; j++)
  13.             if(tab[j] < tab[pmin]) pmin = j;
  14.         swap(tab[i], tab[pmin]); // zamiana indeksow
  15.     }
  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 (zmieniony syntax)
  29.     for(int a : tab)
  30.         cout << a << " ";
  31.  
  32.     // wypisanie elementow po sortowaniu
  33.     cout << endl << "POSORTOWANE\n";
  34.  
  35.     selectionSort(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