Aphitorite's Method

aphitorite's Method sorts stably in $$O(n\log n)$$ comparisons, $$O(\log n)$$ space, and $$O(n)$$ moves in the worst case. aphitorite's Method is also deterministic, unlike other unstable variants, which rely on random distribution of data. It's possible to modify the algorithm such that the amount of space used is $$O(1)$$, which is optimal, however, doing so greatly complicates the current algorithm.

The algorithm uses an semi-implicit data structure to first collect $$O(n/\log^2 n)$$ distinct elements to use as space to sort runs of size $$O(n/\log^2 n)$$ using a hybrid data structure. Using the same space, the algorithm merges those runs in a constant amount of linear passes until the whole list is sorted.

If the algorithm fails to find enough distinct values, aphitorite's Method resorts to its second method of sorting, involving multi-way partitioning all elements using as many collected elements, if $$\log^2 n \leq d < O(n/\log^2 n)$$ distinct elements were found.

If there are $$1 < d < \log^2 n$$ distinct found, then there's not enough elements for the above strategy so aphitorite's Method resorts to multi-way merging and partitioning using the extra allocated $$O(\log n)$$ space instead.

= Explanation =

Buffers
Buffers are crucial for many sorting algorithms, essentially cramming auxiliary space into the main array.

Unique Buffer:
Unique buffers are sections of often adjacent elements that are all unique (that is, no duplicates). Because they are unique, they can be reordered at will and original order can be recovered later by another sort. Because the recovery process is sorting, unique buffers almost always start as sorted subarrays.

Bit Buffer:
A bit buffer is 2 equal ranges of elements that can encode a sequence of bits based on the relative values of the elements in them.

For every element in the first range (call this B), there is one corresponding element in the second range (call this B') and vice versa, as the ranges are the same length.

An element in B cannot be equal to its corresponding element in B'. if it is less, that pair encodes a binary 0. if greater, it encodes a binary 1.

Bit buffers cost $$O(1)$$ operations to set or flip a bit, and $$O(n)$$ (where n is the size of the buffer) to read the contents.

= Pseudocode = wait who put this here--