Dutch National Flag

Dijkstra used the Dutch National Flag Problem* as a structured programming exercise in program derivation and program proof. Given `N' objects coloured red, white or blue, sort them so that objects of the same colour are adjacent, with the colours in the order red, white and blue.

The problem is closely related to the partition operation of quick sort: the attribute need not be a `colour' but can be `greater than the median', or `leading digit is zero', or whatever property you care to choose.

Two Colours

It is easiest to consider just two "colours", {zero,one}, first. The algorithm maintains three sections (possibly empty) in the array a[ ]:

  1. a[1..Lo-1] zeroes
  2. a[Lo..Hi] unknown
  3. a[Hi+1..N] ones

The unknown section is shrunk while maintaining these conditions:

 

  1. Lo := 1; Hi := N;
  2. while Lo <= Hi do
    1. Invariant: a[1..Lo-1] are all zero and a[Hi+1..N] are all one; a[Lo..Hi] are unknown.
    2. if a[Lo] = 0 then Lo++
    3. else swap a[Lo] and a[Hi]; Hi--
--- 2-way Partitioning ---

Part way through the process, some red, white and blue elements are known and are in the "right" place. The section of unknown elements, a[Mid..Hi], is shrunk by examining a[Mid]:

            |
0 0 0 1 1 1 ? ? ? ? 2 2 2
      ^     ^     ^
      |     |     |
      Lo    Mid   Hi

Examine a[Mid]. There are three possibilities: a[Mid] is (0) red, (1) white or (2) blue.
Case (0) a[Mid] is red, swap a[Lo] and a[Mid]; Lo++; Mid++

0 0 0 0 1 1 1 ? ? ? 2 2 2
        ^     ^   ^
        |     |   |
        Lo    Mid Hi

 

Case (1) a[Mid] is white, Mid++

0 0 0 1 1 1 1 ? ? ? 2 2 2
      ^       ^   ^
      |       |   |
      Lo      Mid Hi

 

Case (2) a[Mid] is blue, swap a[Mid] and a[Hi]; Hi--

0 0 0 1 1 1 ? ? ? 2 2 2 2
      ^     ^   ^
      |     |   |
      Lo    Mid Hi

Continue until Mid>Hi.

Demonstration

The data in the HTML FORM below consists of random (0|1|2)s. In the trace, the parts of the array known to the algorithm, i.e. a[1..Mid-1] and a[Hi+1..N] are printed and the unknown section is left blank.
Press the `go' button:

©
L
.
A
l
l
i
s
o
n
(0..2) *:
trace :

 

Complexity

Time

Time complexity is O(n).

Space

Space complexity is O(1).

Notes

  • When the items being sorted are literally just integers in [0..1] or [0..2] one could just count the number of each value and overwrite the array contents accordingly. However the intent is that the array items are reasonably complex and that they happen to have an attribute (~colour) drawn from a 2-value or 3-value data-type.
  • The DNF algorithm can be extended to four, or even more colours but it grows more and more complex to write, and the constant of proportionality in its running time increases.
  • The DNF algorithm can be used to partition an array into sections that are (i) <x (red), (ii) =x (white), and (iii) >x (blue), where x is an estimate of the median, for example. Sections (i) and (iii) can be sorted recursively as in Quick Sort.
  • An alternative to the previous point is to find two different elements, v1 and v2, from the array such that v1<v2 (it's sorted if you can't find such v1 and v2), and partition the array into sections (i) <=v1 (red), (ii) >v1 and <=v2; (white), and (iii) >v2 (blue). Each of the three sections ((iii) might be empty) can then be sorted recursively giving a 3-way Quick Sort.
  • See [radix sort] and [quick sort].
  • * I fondly remember first hearing about the Dutch National Flag problem at the Institute of Computer Science (ICS), London University, c1973.

Exercises

  1. Write a 3-way Quick Sort program, using the DNF algorithm.
  2. Write a "flag" program for four "colours".

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值