From 2438238eb5653e860d1728581341036b4c6bcc20 Mon Sep 17 00:00:00 2001 From: Henrique Gemignani Passos Lima Date: Sun, 27 Nov 2022 18:02:31 +0200 Subject: [PATCH] Remove extra compare in rstl::sort Former-commit-id: 8c999d9a88e8febe1f056858fbe267d231e1c94a --- include/rstl/algorithm.hpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/rstl/algorithm.hpp b/include/rstl/algorithm.hpp index 124974d4..5f4702e1 100644 --- a/include/rstl/algorithm.hpp +++ b/include/rstl/algorithm.hpp @@ -73,17 +73,18 @@ void sort(It first, It last, Cmp cmp) { __insertion_sort(first, last, cmp); } else { It beforeLast = last - 1; - __sort3(*first, *(first + count / 2), *(last - 1), cmp); - 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, *middle); ++it) + for (; cmp(*it, value); ++it) ; - for (; cmp(*middle, *beforeLast); --beforeLast) + for (; cmp(value, *beforeLast); --beforeLast) ; - if (!cmp(*it, *beforeLast)) + if (it > beforeLast) break; rstl::swap(*it, *beforeLast); ++it;