Timsort
Find stretches already in order and join them instead of sorting every value from scratch.
A run may rise or stay level. Reverse a falling run only while it falls strictly, so equal keys keep their order.
To compute minrun, halve the length until it drops below 8 and round up any discarded remainder. Three consecutive wins trigger galloping. This lesson fixes the threshold at 3; production versions can adapt it.
An ordered input takes O(n) time. Worst-case time is O(n log n), with O(n) extra space.