Further improve rstl::sort

Thanks again GibHaltmannKill
This commit is contained in:
Henrique Gemignani Passos Lima 2022-11-28 18:03:59 +02:00
parent 906d8406ab
commit 8efda4623a
No known key found for this signature in database
GPG Key ID: E224F951761145F8
1 changed files with 4 additions and 5 deletions

View File

@ -72,9 +72,10 @@ void sort(It first, It last, Cmp cmp) {
if (count <= 20) { if (count <= 20) {
__insertion_sort(first, last, cmp); __insertion_sort(first, last, cmp);
} else { } else {
It pivot = first + count / 2;
It end = last; It end = last;
__sort3(*first, *(first + count / 2), *--end, cmp); __sort3(*first, *pivot, *--end, cmp);
typename iterator_traits< It >::value_type value = *(first + count / 2); typename iterator_traits< It >::value_type value = *pivot;
It it = first + 1; It it = first + 1;
--end; --end;
while (true) { while (true) {
@ -84,9 +85,7 @@ void sort(It first, It last, Cmp cmp) {
--end; --end;
if (it >= end) if (it >= end)
break; break;
iter_swap(it, end); iter_swap(it++, end--);
++it;
--end;
} }
sort(first, it, cmp); sort(first, it, cmp);
sort(it, last, cmp); sort(it, last, cmp);