user
File:Dutch flag 3.jpg

The Dutch national flag is composed of three coloured bands in the order:

:   :   * red     (top)
        * then white,   and
        * lastly blue   (at the bottom).

The problem posed by Edsger Dijkstra is:

:   Given a number of red, blue and white balls in random order, arrange them in the order of the colours in the Dutch national flag.

When the problem was first posed, Dijkstra then went on to successively refine a solution, minimising the number of swaps and the number of times the colour of a ball needed to determined and restricting the balls to end in an array, ...

Task

1. Generate a randomized order of balls *ensuring that they are not in the order of the Dutch national flag*.
2. Sort the balls in a way idiomatic to your language.
3. Check the sorted balls *are* in the order of the Dutch national flag.

C.f.

* Dutch national flag problem
* Probabilistic analysis of algorithms for the Dutch national flag problem by Wei-Mei Chen. (pdf)
assistant
(formerly Perl 6)
Here are five ways to do it, all one liners (apart from the test apparatus).

```
enum NL <red white blue>;
my @colors;

sub how'bout (&this-way) {
    sub show {
        say @colors;
        say "Ordered: ", [<=] @colors;
    }

    @colors = NL.roll(20);
    show;
    this-way;
    show;
    say '';
}

say "Using functional sort";
how'bout { @colors = sort *.value, @colors }

say "Using in-place sort";
how'bout { @colors .= sort: *.value }

say "Using a Bag";
how'bout { @colors = flat red, white, blue Zxx bag(@colors».key)<red white blue> }

say "Using the classify method";
how'bout { @colors = flat (.list for %(@colors.classify: *.value){0,1,2}) }

say "Using multiple greps";
how'bout { @colors = flat (.grep(red), .grep(white), .grep(blue) given @colors) }

```

Output:

```
Using functional sort
red red white white red red red red red red red white red white red red red white white white
Ordered: False
red red red red red red red red red red red red red white white white white white white white
Ordered: True

Using in-place sort
red blue white red white blue white blue red white blue blue blue red white white red blue red blue
Ordered: False
red red red red red red white white white white white white blue blue blue blue blue blue blue blue
Ordered: True

Using a Bag
red blue blue blue white red white red white blue blue red red red red blue blue red white blue
Ordered: False
red red red red red red red red white white white white blue blue blue blue blue blue blue blue
Ordered: True

Using the classify method
blue red white blue blue white white red blue red red white red blue white white red blue red white
Ordered: False
red red red red red red red white white white white white white white blue blue blue blue blue blue
Ordered: True

Using multiple greps
red white blue white white red blue white red white red white white white white white red red blue red
Ordered: False
red red red red red red red white white white white white white white white white white blue blue blue
Ordered: True
```