Dutch national flag problem

From Wikipedia, the free encyclopedia
For the article about the Dutch national flag, see  Flag of the Netherlands.
The Dutch national flag

The Dutch national flag problem (DNF)[1] is a computer science programming problem proposed by Edsger Dijkstra.[2] The flag of the Netherlands consists of three colours: red, white and blue. Given balls of these three colours arranged randomly in a line (the actual number of balls does not matter), the task is to arrange them such that all balls of the same colour are together and their collective colour groups are in the correct order.

The solution to this problem is of interest for designing sorting algorithms. In particular, variants of the quicksort algorithm that must be robust to repeated elements need 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.[3]

The array case[edit]

This problem can also be viewed in terms of rearranging elements of an array. Suppose each of the possible elements could be classified into exactly one of three categories (bottom, middle, and top). For example, if all elements are in 0 ... 1, the bottom could be defined as elements in 0 ... 0.1 (not including 0.1), the middle as 0.1 ... 0.3 (not including 0.3) and the top as 0.3 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 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.[4] 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.[1]

Pseudocode[edit]

The following pseudocode for three-way partitioning assumes zero-based array indexing. It uses three indices ij and n, maintaining the invariant that i ≤ jn holds the boundary of numbers greater than midj is the position of number under consideration. And i is the boundary for the numbers lesser than the mid.


procedure three-way-partition(A : array of values, mid : value):
    i ← 0
    j ← 0
    n ← size of A - 1

    while j ≤ n:
        if A[j] < mid:
            swap A[i] and A[j]
            i ← i + 1
            j ← j + 1
        else if A[j] > mid:
            swap A[j] and A[n]
            n ← n - 1
        else:
            j ← j + 1

Note that j will be greater than i only if the mid is hit.

See also[edit]

References[edit]

  1. Jump up to:a b "Dutch National Flag problem and algorithm". Faculty of Information Technology (Clayton), Monash University, Australia. 1998.
  2. Jump up^ In a chapter of his book A Discipline of Programming Prentice-Hall, 1976
  3. Jump up^ The latter case occurs in string sorting with multi-key quicksortKim, Eunsang; Park, Kunsoo (2009). "Improving multikey Quicksort for sorting strings with many equal elements". Information Processing Letters 109 (9): 454–459. doi:10.1016/j.ipl.2009.01.007.
  4. Jump up^ Black, Paul E. "Dutch national flag"Dictionary of Algorithms and Data StructuresNIST.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值