Advertisement
sst311212

selection sort

Dec 13th, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void swap(int &a,int &b)
  7. {
  8.      int temp;
  9.      temp = b;
  10.      b = a;
  11.      a = temp;
  12. }
  13.  
  14. int main()
  15. {
  16.     int flag=0;
  17.     int array[]={7,1,5,9,2};
  18.    
  19.     for (int i=0;i<5;i++){
  20.         flag = i;
  21.         for (int j=i+1;j<5;j++){
  22.             if (array[flag] > array[j]){
  23.                 flag = j;
  24.             }
  25.         }
  26.     swap(array[flag],array[i]);
  27.     cout<<array[i]<<" ";
  28.     }
  29.     cout<<endl;
  30.     system("PAUSE");
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement