fork download
  1. #include <iostream>
  2. #include <iterator>
  3.  
  4.  
  5. template<typename T, typename Cmp>
  6. void ssort(T* f, T* l, Cmp cmp){
  7. T* p, *i, t;
  8. for(;f != l; ++f){
  9. p = f;
  10. for(i = f + 1; i < l; ++i){
  11. if(cmp(*i, *p))
  12. p = i;
  13. }
  14.  
  15. if(p != f){
  16. t = *p;
  17. *p = *f;
  18. *f = t;
  19. }
  20. }
  21. }
  22.  
  23.  
  24.  
  25. int main(void){
  26. int arr[] = { 5, 7, 3, 9, 4, 1, 8, 6, 2 };
  27. int num = sizeof(arr)/sizeof(arr[0]);
  28.  
  29. std::ostream_iterator<int> lp(std::cout, " ");
  30. std::copy(arr, arr + num, lp);
  31. std::cout << std::endl;
  32.  
  33. ssort(arr, arr + num, [] (const int& a, const int& b){
  34. return (a > b);
  35. });
  36.  
  37. std::copy(arr, arr + num, lp);
  38. return 0;
  39. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
5 7 3 9 4 1 8 6 2 
9 8 7 6 5 4 3 2 1