A variation of Quicksort algorithm for in-place sorting of data that does not completely fit into RAM.
It is an external sort on a file that is organized into blocks, each of which hold equal number of records. The
blocks can be accessed randomly using their indices.
A step of a comparison algorithm takes two records to compare and conditionally swaps them. The corresponding
external algorithm on the other hand, will take two blocks of records to fill one continuous buffer in memory.
Instead of swapping, it runs a full internal sorting algorithm on the buffer and then blocks are written back
to the file before the corresponding block pointer is moved.
For an example implementation, we take the widely known Quicksort as the internal sorting algorithm to externalize. To recall the steps and clarify the terms used in this document, here is a short description of the original sorting algorithm.
Applying the transformation on Quicksort, the two pointers of the partitioning become block indices but pivot stays one record.
An idea specific for the Quicksort is that partitioning can be started without selecting the final pivot.
The pivot can be changed while partitioning, it just needs to stay in an interval to meet pivot conditions (i.e. all keys in the left partition <= pivot <= all keys in the right partition.)
Partitioning starts with reading the lowest and the highest block into the buffer.
Pivot selection saves block reads by selecting a key that is anyway in the memory. Although this may not be an
optimal pivot, the implementation constantly optimizes pivot while partitioning. It is non-trivial to construct
an input file for the worst case when the algorithm would always choose extreme pivots and run for N square time.