fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. template <typename T> void selection_sort(T* begin, T* end, unsigned pos = 0u)
  5. {
  6. if(begin != end)
  7. {
  8. for(int i = pos; i < (end - begin) + pos; i++)
  9. {
  10. if(begin[pos] > begin[i]) std::swap(begin[pos], begin[i]);
  11. }
  12. selection_sort(begin + 1, end, pos);
  13. }
  14. }
  15.  
  16. int main(int argc, char** argv)
  17. {
  18. int itens[] = {1, 3, 5, 2, 4};
  19.  
  20. selection_sort(&itens[0], &itens[5], 0);
  21.  
  22. for(int i = 0; i < 5; i++)
  23. {
  24. std::cout << itens[i] << " ";
  25. }
  26.  
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
1 2 3 4 5