
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
 Generate a randomized order of balls ensuring that they are not in the order of the Dutch national flag.
 Sort the balls in a way idiomatic to your language.
 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)


// version 1.1.4
 
import java.util.Random
 
enum class DutchColors { RED, WHITE, BLUE }
 
fun Array<DutchColors>.swap(i: Int, j: Int) {
    val temp = this[i]
    this[i] = this[j]
    this[j] = temp
}
 
fun Array<DutchColors>.sort() {
    var lo = 0
    var mid = 0
    var hi = this.lastIndex
 
    while (mid <= hi) {
        when (this[mid]) {
            DutchColors.RED   -> this.swap(lo++, mid++)
            DutchColors.WHITE -> mid++
            DutchColors.BLUE  -> this.swap(mid, hi--)
        }
    }
}
 
fun Array<DutchColors>.isSorted(): Boolean {
    return (1 until this.size)
        .none { this[it].ordinal < this[it - 1].ordinal }
}
 
const val NUM_BALLS = 9
 
fun main(args: Array<String>) {
    val r = Random()
    val balls  = Array(NUM_BALLS) { DutchColors.RED }
    val colors = DutchColors.values()
 
    // give balls random colors whilst ensuring they're not already sorted
    do {
        for (i in 0 until NUM_BALLS) balls[i] = colors[r.nextInt(3)]
    }
    while (balls.isSorted())
 
    // print the colors of the balls before sorting
    println("Before sorting : ${balls.contentToString()}")
 
    // sort the balls in DutchColors order
    balls.sort()
 
    // print the colors of the balls after sorting
    println("After sorting  : ${balls.contentToString()}")
}