
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)


COLORS=(red white blue)
 
# to go from name to number, we make variables out of the color names 
# (e.g. the variable "$red" has value "1").
for (( i=0; i<${#COLORS[@]}; ++i )); do
  eval ${COLORS[i]}=$i
done
 
# Make a random list
function random_balls {
  local -i n="$1"
  local -i i
  local balls=()
  for (( i=0; i < n; ++i )); do
    balls+=("${COLORS[RANDOM%${#COLORS[@]}]}")
  done
  echo "${balls[@]}"
}
 
# Test for Dutchness
function dutch? {
  if (( $# < 2 )); then
     return 0
  else
    local first="$1"
    shift
    if eval "(( $first > $1 ))"; then
      return 1
    else
      dutch? "$@"
    fi
  fi
}
 
# Sort into order
function dutch {
  local -i lo=-1 hi=$# i=0
  local a=("$@")
  while (( i < hi )); do
    case "${a[i]}" in
      red)
        let lo+=1
        local t="${a[lo]}"
        a[lo]="${a[i]}"
        a[i]="$t"
        let i+=1
        ;;
      white) let i+=1;;
      blue)
        let hi-=1
        local t="${a[hi]}"
        a[hi]="${a[i]}"
        a[i]="$t"
        ;;
    esac
  done
  echo "${a[@]}"
}