General Purpose library for Freestanding C++ and POSIX systems
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

44 行
1.1 KiB

#pragma once
#include <gp/algorithms/move.hpp>
namespace gp {
template<typename it_t, typename pred>
it_t lomuto_partition(it_t first, it_t last, pred predicate = pred{}) {
auto pivot = *--it_t(last);
auto i = --it_t(first);
for(auto j = first; j != last; ++j) {
if(!predicate(*j, pivot)){
++i;
gp::swap(*i, *j);
}
}
return i;
}
template<typename it_t, typename pred>
it_t hoare_partition(it_t first, it_t last, pred predicate = pred{}) {
while(first != last) {
while(predicate(*first)) {
++first;
if(first == last) return first;
}
do {
--last;
if(first == last) return first;
} while(!predicate(*last));
swap(*first, *last);
++first;
}
return first;
}
/**
template<typename it_t, typename pred>
it_t partition(it_t first, it_t last, pred predicate = pred{}) {
return gp::hoare_partition(first, last, predicate);
}**/
}