Dutch National Flag Algorithm
A sorting algorithm designed for sorting an array that has only three unique entries.
Generally modeled as
The goal is to sort it faster than traditional sorting algorithms that use
The method is to maintain a low, mid and high pointers.
The low and high pointers maintain the boundary for the lowest and highest value observation,
The mid pointer traverses the list and swaps element
- If
, swap with low, increment both low and mid - If
, swap with high, increment mid, decrement high. - If
, Do nothing.
It achieves a time complexity of
A modified version is used in Quicksort with huge datasets that often involve multiple duplicate keys.