Improve sort

Former-commit-id: 26621f44eb
This commit is contained in:
Henrique Gemignani Passos Lima 2022-11-27 23:58:19 +02:00
parent 2438238eb5
commit 2672de72d8
1 changed files with 8 additions and 8 deletions

View File

@ -67,7 +67,7 @@ void __sort3(T& a, T& b, T& c, Cmp comp) {
template < typename It, class Cmp > template < typename It, class Cmp >
void sort(It first, It last, Cmp cmp) { void sort(It first, It last, Cmp cmp) {
int count = last - first; int count = last - first; // use distance?
if (count > 1) { if (count > 1) {
if (count <= 20) { if (count <= 20) {
__insertion_sort(first, last, cmp); __insertion_sort(first, last, cmp);
@ -75,18 +75,18 @@ void sort(It first, It last, Cmp cmp) {
It beforeLast = last - 1; It beforeLast = last - 1;
It middle = first + count / 2; It middle = first + count / 2;
__sort3(*first, *middle, *(last - 1), cmp); __sort3(*first, *middle, *(last - 1), cmp);
typename iterator_traits< It >::value_type value = *middle;
typename iterator_traits< It >::value_type value = *middle;
It it = first + 1; It it = first + 1;
while (true) { while (true) {
for (; cmp(*it, value); ++it) while (cmp(*it, value))
; ++it;
for (; cmp(value, *beforeLast); --beforeLast) while (cmp(value, *beforeLast))
; --beforeLast;
if (it > beforeLast) if (it >= beforeLast)
break; break;
rstl::swap(*it, *beforeLast); iter_swap(beforeLast, it); // wrong order?
++it; ++it;
--beforeLast; --beforeLast;
} }