From 26621f44eba2955e3e2966f28b544f62449f3157 Mon Sep 17 00:00:00 2001 From: Henrique Gemignani Passos Lima Date: Sun, 27 Nov 2022 23:58:19 +0200 Subject: [PATCH] Improve sort --- include/rstl/algorithm.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/rstl/algorithm.hpp b/include/rstl/algorithm.hpp index 5f4702e1..5765cf47 100644 --- a/include/rstl/algorithm.hpp +++ b/include/rstl/algorithm.hpp @@ -67,7 +67,7 @@ void __sort3(T& a, T& b, T& c, Cmp comp) { template < typename It, class Cmp > void sort(It first, It last, Cmp cmp) { - int count = last - first; + int count = last - first; // use distance? if (count > 1) { if (count <= 20) { __insertion_sort(first, last, cmp); @@ -75,18 +75,18 @@ void sort(It first, It last, Cmp cmp) { It beforeLast = last - 1; It middle = first + count / 2; __sort3(*first, *middle, *(last - 1), cmp); + typename iterator_traits< It >::value_type value = *middle; - It it = first + 1; while (true) { - for (; cmp(*it, value); ++it) - ; - for (; cmp(value, *beforeLast); --beforeLast) - ; - if (it > beforeLast) + while (cmp(*it, value)) + ++it; + while (cmp(value, *beforeLast)) + --beforeLast; + if (it >= beforeLast) break; - rstl::swap(*it, *beforeLast); + iter_swap(beforeLast, it); // wrong order? ++it; --beforeLast; }