BAN: implement quick sort and test for it

This commit is contained in:
Bananymous 2023-12-07 11:17:05 +02:00
parent 59ad639fa8
commit 6caa9b6f95
2 changed files with 41 additions and 0 deletions

View File

@ -15,6 +15,43 @@ namespace BAN
swap(*lhs, *rhs);
}
namespace detail
{
template<typename It, typename Comp>
It sort_quick_partition(It begin, It end, Comp comp)
{
It pivot = end; --pivot;
It it1 = begin;
for (It it2 = begin; it2 != pivot; ++it2)
{
if (comp(*it2, *pivot))
{
swap(*it1, *it2);
++it1;
}
}
swap(*it1, *pivot);
return it1;
}
}
template<typename It, typename Comp = less<typename It::value_type>>
void sort_quick(It begin, It end, Comp comp = {})
{
{
It it = begin;
if (it == end || ++it == end)
return;
}
It mid = detail::sort_quick_partition(begin, end, comp);
sort_quick(begin, mid, comp);
sort_quick(++mid, end, comp);
}
}

View File

@ -34,4 +34,8 @@ int main()
TEST("exchange sort", BAN::sort_exchange, 100);
TEST("exchange sort", BAN::sort_exchange, 1000);
TEST("exchange sort", BAN::sort_exchange, 10000);
TEST("quick sort", BAN::sort_quick, 100);
TEST("quick sort", BAN::sort_quick, 1000);
TEST("quick sort", BAN::sort_quick, 10000);
}