
The Dutch national flag problem
is a
computational problem
In theoretical computer science, a computational problem is a problem that may be solved by an algorithm. For example, the problem of factoring
:"Given a positive integer ''n'', find a nontrivial prime factor of ''n''."
is a computational probl ...
proposed by
Edsger Dijkstra
Edsger Wybe Dijkstra ( ; ; 11 May 1930 – 6 August 2002) was a Dutch computer scientist, programmer, software engineer, systems scientist, and science essayist. He received the 1972 Turing Award for fundamental contributions to developing progr ...
.
[In a chapter of his book ''A Discipline of Programming'' Prentice-Hall, 1976] The
flag of the Netherlands
The national flag of the Netherlands ( nl, de Nederlandse vlag) is a horizontal tricolour of red, white, and blue. The current design originates as a variant of the late 16th century orange-white-blue ''Prinsenvlag'' ("Prince's Flag"), evol ...
consists of three colors: red, white, and blue. Given balls of these three colors arranged randomly in a line (it does not matter how many balls there are), the task is to arrange them such that all balls of the same color are together and their collective color groups are in the correct order.
The solution to this problem is of interest for designing
sorting algorithm
In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is importan ...
s; in particular, variants of the
quicksort
Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
algorithm that must be
robust to repeated elements may use a three-way partitioning function that groups items less than a given key (red), equal to the key (white) and greater than the key (blue). Several solutions exist that have varying performance characteristics, tailored to sorting arrays with either small or large numbers of repeated elements.
[The latter case occurs in ]string
String or strings may refer to:
*String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects
Arts, entertainment, and media Films
* ''Strings'' (1991 film), a Canadian anim ...
sorting with multi-key quicksort.
The array case
This problem can also be viewed in terms of rearranging elements of an
array
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
.
Suppose each of the possible elements could be classified into exactly one of three categories (bottom, middle, and top).
For example, if all the elements are in 0 ... 1, the bottom could be defined as elements in 0 ... 0.25 (not including 0.25), the middle as 0.25 ... 0.5 (not including 0.5)
and the top as 0.5 and greater. (The choice of these values illustrates that the categories need not be equal ranges). The problem is then to produce an array such that all "bottom" elements come before (have an index less than the index of) all "middle" elements, which come before all "top" elements.
One
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
is to have the top group grow down from the top of the array, the bottom group grow up from the bottom, and keep the middle group just above the bottom. The algorithm indexes three locations, the bottom of the top group, the top of the bottom group, and the top of the middle group. Elements that are yet to be sorted fall between the middle and the top group.
At each step, examine the element just above the middle. If it belongs to the top group, swap it with the element just below the top. If it belongs in the bottom, swap it with the element just above the bottom. If it is in the middle, leave it. Update the appropriate index. Complexity is Θ(n) moves and examinations.
Pseudocode
The following
pseudocode
In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
for three-way partitioning which assumes zero-based array indexing was proposed by Dijkstra himself.
It uses three indices , and , maintaining the
invariant that .
* Entries from 0 up to (but not including) are values less than ,
* entries from up to (but not including) are values equal to ,
* entries from up to (and including) are values not yet sorted, and
* entries from to the end of the array are values greater than .
procedure three-way-partition(A : array of values, mid : value):
i ← 0
j ← 0
k ← size of A - 1
while j <= k:
if A
< mid:
swap A
and A
i ← i + 1
j ← j + 1
else if A
> mid:
swap A
and A
k ← k - 1
else:
j ← j + 1
See also
*
American flag sort
An American flag sort is an efficient, in-place variant of radix sort that distributes items into buckets. Non-comparative sorting algorithms such as radix sort and American flag sort are typically used to sort large objects such as strings, for ...
References
{{Reflist
External links
Explanation and interactive explanatory execution of the algorithm sorting two or three colors
Sorting algorithms
Computational problems
Edsger W. Dijkstra
Articles with example pseudocode