fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. #include<map>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7.  
  8. int find_min(int ar[],int s, int size)
  9. {
  10. int index = s-1;
  11. bool found = false;
  12. for(int i = s; i < size; i++)
  13. {
  14. if(ar[index] > ar[i])
  15. {
  16. index = i;
  17. found = true;
  18. }
  19. }
  20.  
  21. if (found)
  22. {
  23. return index;
  24. }
  25. else
  26. return -1;
  27. }
  28.  
  29. void exchange(int &x, int& y)
  30. {
  31. int t=x;
  32. x=y;
  33. y=t;
  34. }
  35. void selection_sort(int arr[], int size)
  36. {
  37. int loc;
  38. for(int count = 0; count < size-1; count++)
  39. {
  40. loc = find_min(arr, count+1,size);
  41. if (loc >= 0)
  42. {
  43. exchange(arr[count], arr[loc]);
  44. }
  45. }
  46. }
  47.  
  48. int main(){
  49.  
  50. int arr[] ={9,8,7,6,5,4,3,2,1};
  51. selection_sort(arr,9);
  52. for(int i:arr)
  53. std::cout<<i<<" ";
  54. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9