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.

43 line
1.1 KiB

  1. #pragma once
  2. #include <gp/algorithms/move.hpp>
  3. namespace gp {
  4. template<typename it_t, typename pred>
  5. it_t lomuto_partition(it_t first, it_t last, pred predicate = pred{}) {
  6. auto pivot = *--it_t(last);
  7. auto i = --it_t(first);
  8. for(auto j = first; j != last; ++j) {
  9. if(!predicate(*j, pivot)){
  10. ++i;
  11. gp::swap(*i, *j);
  12. }
  13. }
  14. return i;
  15. }
  16. template<typename it_t, typename pred>
  17. it_t hoare_partition(it_t first, it_t last, pred predicate = pred{}) {
  18. while(first != last) {
  19. while(predicate(*first)) {
  20. ++first;
  21. if(first == last) return first;
  22. }
  23. do {
  24. --last;
  25. if(first == last) return first;
  26. } while(!predicate(*last));
  27. swap(*first, *last);
  28. ++first;
  29. }
  30. return first;
  31. }
  32. /**
  33. template<typename it_t, typename pred>
  34. it_t partition(it_t first, it_t last, pred predicate = pred{}) {
  35. return gp::hoare_partition(first, last, predicate);
  36. }**/
  37. }