From 19604015de1bc48ee0b83430b039eee87628db1e Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 7 Dec 2023 19:28:31 +0200 Subject: [PATCH] BAN: Implement quick sort --- BAN/include/BAN/Sort.h | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/BAN/include/BAN/Sort.h b/BAN/include/BAN/Sort.h index 22af766dda..46b2f9f876 100644 --- a/BAN/include/BAN/Sort.h +++ b/BAN/include/BAN/Sort.h @@ -43,15 +43,26 @@ namespace BAN template> void sort_quick(It begin, It end, Comp comp = {}) { - { - It it = begin; - if (it == end || ++it == end) - return; - } - + if (begin == end || next(begin, 1) == end) + return; It mid = detail::sort_quick_partition(begin, end, comp); sort_quick(begin, mid, comp); sort_quick(++mid, end, comp); } + template> + void sort_insertion(It begin, It end, Comp comp = {}) + { + if (begin == end) + return; + for (It it1 = next(begin, 1); it1 != end; ++it1) + { + typename It::value_type x = move(*it1); + It it2 = it1; + for (; it2 != begin && comp(x, *prev(it2, 1)); --it2) + *it2 = move(*prev(it2, 1)); + *it2 = move(x); + } + } + }