The Dutch national flag problem exposes an example of an input for which quicksort performs poorly. When there are many duplicates in the array to be sorted, it will often be the case that unecessary function calls to the recursive sort()
method will be made on subarrays that already contain elements that are all equal.
The three bars of the Dutch National Flag hint towards another way of thinking about partitioning an unsorted array: move elements around such that elements we have all the elements less than pivot element, followed by all elements equal to the pivot element, and then all elements greater than the pivot element. In the case of an unsorted array of just three different values, say, red, white, and blue, we can imagine that sorting these shuffled values around would lead to a final structure much like the Dutch National Flag.
This implementation of quicksort uses Dijkstra's clever partitioning algorithm to expand regions of values less than and greater than the pivot element from the left and right ends, respectively, of the subarray to be sorted.
https://d3js.org/d3.v3.min.js