From 94e6b9fa655befc5d2fbd79ccff12481f5a5bcb8 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 7 Dec 2023 23:56:11 +0200 Subject: [PATCH] BAN: Implement intro sort --- BAN/include/BAN/Sort.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/BAN/include/BAN/Sort.h b/BAN/include/BAN/Sort.h index 294a6209..93168de6 100644 --- a/BAN/include/BAN/Sort.h +++ b/BAN/include/BAN/Sort.h @@ -2,6 +2,7 @@ #include #include +#include namespace BAN { @@ -100,4 +101,28 @@ namespace BAN } } + namespace detail::sort + { + + template + void intro_impl(It begin, It end, size_t max_depth, Comp comp) + { + if (distance(begin, end) < 16) + return sort_insertion(begin, end, comp); + if (max_depth == 0) + return sort_heap(begin, end, comp); + It mid = detail::sort::partition(begin, end, comp); + intro_impl(begin, mid, max_depth - 1, comp); + intro_impl(++mid, end, max_depth - 1, comp); + } + + } + + template> + void sort_intro(It begin, It end, Comp comp = {}) + { + size_t max_depth = Math::ilog2(distance(begin, end)); + detail::sort::intro_impl(begin, end, max_depth, comp); + } + }