Sugar Coat 5991 sort

Sugar Coat 5991 Sort (a.k.a. Soda Soiree) is the second fast sort ever, behind Crunchy Courtyard Sort.

It is an out-of-place stable variation of quicksort which reduces the number of moves from regular stable quicksort.

Between each level of recursion, it alternates partitioning from the array into the buffer, and from the buffer into the array. This is done to reduce the number of array accesses, faciliating in the speed of the forked TSoS visualisation.

To handle equal items, it selects a random split value, and all items that come before it go to the first partition and after it goes to the second partition. This ensured O(n log n) average case for every array, but there is still an O(∞) worst case.

If there are less than 16 items, it chooses a random pivot, otherwise it chooses the median of three random items.

Sugar Coat 5991 Sort also has special cases for 4 or less items. If the items are in the array, it sorts those items in the array. If the items are in the buffer, it sorts and moves the items into the array.