From 237b8f77d971aef9dc9fd6422c06e42a5ce72879 Mon Sep 17 00:00:00 2001 From: Henrique Gemignani Passos Lima Date: Mon, 28 Nov 2022 02:40:31 +0200 Subject: [PATCH] rstl::sort is just a few regswaps away Former-commit-id: ef221d5c96fc75578e4843a7d0247a2235f22aa3 --- include/rstl/algorithm.hpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/include/rstl/algorithm.hpp b/include/rstl/algorithm.hpp index 5765cf47..80703b14 100644 --- a/include/rstl/algorithm.hpp +++ b/include/rstl/algorithm.hpp @@ -72,23 +72,21 @@ void sort(It first, It last, Cmp cmp) { if (count <= 20) { __insertion_sort(first, last, cmp); } else { - It beforeLast = last - 1; - It middle = first + count / 2; - __sort3(*first, *middle, *(last - 1), cmp); - - typename iterator_traits< It >::value_type value = *middle; + It end = last; + __sort3(*first, *(first + count / 2), *--end, cmp); + typename iterator_traits< It >::value_type value = *(first + count / 2); It it = first + 1; - + --end; while (true) { while (cmp(*it, value)) ++it; - while (cmp(value, *beforeLast)) - --beforeLast; - if (it >= beforeLast) + while (cmp(value, *end)) + --end; + if (it >= end) break; - iter_swap(beforeLast, it); // wrong order? + iter_swap(it, end); ++it; - --beforeLast; + --end; } sort(first, it, cmp); sort(it, last, cmp);