Advertisement
bolji_programer

Algoritmi za sortiranje - Selection sort

Jan 1st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int n;
  8.     cin>>n;
  9.  
  10.     int A[n];
  11.  
  12.     for (int i=0;i<n;i++)
  13.         cin>>A[i];
  14.  
  15.     for (int i=0;i<n-1;i++)
  16.     {
  17.         int mi=A[i];
  18.         int pos=i;
  19.  
  20.         for (int j=i+1;j<n;j++)
  21.         {
  22.             if (A[j]<mi)
  23.             {
  24.                 mi=A[j];
  25.                 pos=j;
  26.             }
  27.         }
  28.  
  29.         swap(A[i],A[pos]);
  30.     }
  31.  
  32.     for (int i=0;i<n;i++)
  33.         cout<<A[i]<<' ';
  34.     cout<<endl;
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement