General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
664 B

  1. #pragma once
  2. #include <gp/algorithms/move.hpp>
  3. namespace gp {
  4. template<typename it_t, typename pred>
  5. void selection_sort(it_t first, it_t last, pred predicate = pred{}) {
  6. while(first != last) {
  7. auto traveler = first;
  8. auto it = first;
  9. it++;
  10. for(;it!=last;it++) {
  11. if(predicate(*it, *traveler)) traveler = it;
  12. }
  13. gp::swap(*first, *traveler);
  14. first++;
  15. }
  16. }
  17. template<typename it_t, typename pred>
  18. void sort(it_t first, it_t last, pred predicate = pred{}) {
  19. return selection_sort(first, last, predicate);
  20. }
  21. }