Dutch national flag problem: Difference between revisions

m
m (typo)
 
(153 intermediate revisions by 70 users not shown)
Line 1:
{{task}}
[[File:Dutch_flag_3.jpg|350px||right]]
The Dutch national flag is composed of three coloured bands in the order red then white and lastly blue. The problem posed by [[wp:Edsger Dijkstra|Edsger Dijkstra]] is:
 
:Given a number of red, blue and white balls in random order, arrange them in the order of the colours Dutch national flag.
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 [[wp:Edsger Dijkstra|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, ...
 
 
;This task is to:
;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.
 
 
;Cf.
;C.f.:
* [[wp:Dutch national flag problem|Dutch national flag problem]]
* [https://www.google.co.uk/search?rlz=1C1DSGK_enGB472GB472&sugexp=chrome,mod=8&sourceid=chrome&ie=UTF-8&q=Dutch+national+flag+problem#hl=en&rlz=1C1DSGK_enGB472GB472&sclient=psy-ab&q=Probabilistic+analysis+of+algorithms+for+the+Dutch+national+flag+problem&oq=Probabilistic+analysis+of+algorithms+for+the+Dutch+national+flag+problem&gs_l=serp.3...60754.61818.1.62736.1.1.0.0.0.0.72.72.1.1.0...0.0.Pw3RGungndU&psj=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=c33d18147f5082cc&biw=1395&bih=951 Probabilistic analysis of algorithms for the Dutch national flag problem] by Wei-Mei Chen. (pdf)
<br><br>
 
=={{header|11l}}==
{{trans|Python: Construct from ball counts}}
 
<syntaxhighlight lang="11l">V colours_in_order = ‘Red White Blue’.split(‘ ’)
 
F dutch_flag_sort3(items)
[String] r
L(colour) :colours_in_order
r.extend([colour] * items.count(colour))
R r
 
V balls = [‘Red’, ‘Red’, ‘Blue’, ‘Blue’, ‘Blue’, ‘Red’, ‘Red’, ‘Red’, ‘White’, ‘Blue’]
print(‘Original Ball order: ’balls)
V sorted_balls = dutch_flag_sort3(balls)
print(‘Sorted Ball Order: ’sorted_balls)</syntaxhighlight>
 
{{out}}
<pre>
Original Ball order: [Red, Red, Blue, Blue, Blue, Red, Red, Red, White, Blue]
Sorted Ball Order: [Red, Red, Red, Red, Red, White, Blue, Blue, Blue, Blue]
</pre>
 
=={{header|ABAP}}==
This works for ABAP Version 7.40 and above, the color blue is excluded as an option for the last entry to insure an unsorted sequence.
 
<syntaxhighlight lang="abap">
report z_dutch_national_flag_problem.
 
interface sorting_problem.
methods:
generate_unsorted_sequence
importing
lenght_of_sequence type int4
returning
value(unsorted_sequence) type string,
 
sort_sequence
changing
sequence_to_be_sorted type string,
 
is_sorted
importing
sequence_to_check type string
returning
value(sorted) type abap_bool.
endinterface.
 
 
class dutch_national_flag_problem definition.
public section.
interfaces:
sorting_problem.
 
 
constants:
begin of dutch_flag_colors,
red type char1 value 'R',
white type char1 value 'W',
blue type char1 value 'B',
end of dutch_flag_colors.
endclass.
 
 
class dutch_national_flag_problem implementation.
method sorting_problem~generate_unsorted_sequence.
data(random_int_generator) = cl_abap_random_int=>create(
seed = cl_abap_random=>seed( )
min = 0
max = 2 ).
 
do lenght_of_sequence - 1 times.
data(random_int) = random_int_generator->get_next( ).
 
data(next_color) = cond char1(
when random_int eq 0 then dutch_flag_colors-red
when random_int eq 1 then dutch_flag_colors-white
when random_int eq 2 then dutch_flag_colors-blue ).
 
unsorted_sequence = |{ unsorted_sequence }{ next_color }|.
enddo.
 
if strlen( unsorted_sequence ) > 0.
random_int = random_int_generator->get_next( ).
 
next_color = cond char1(
when random_int eq 0 or random_int eq 2 then dutch_flag_colors-red
when random_int eq 1 then dutch_flag_colors-white ).
 
unsorted_sequence = |{ unsorted_sequence }{ next_color }|.
endif.
endmethod.
 
 
method sorting_problem~sort_sequence.
data(low_index) = 0.
data(middle_index) = 0.
data(high_index) = strlen( sequence_to_be_sorted ) - 1.
 
while middle_index <= high_index.
data(current_color) = sequence_to_be_sorted+middle_index(1).
 
if current_color eq dutch_flag_colors-red.
data(buffer) = sequence_to_be_sorted+low_index(1).
 
sequence_to_be_sorted = replace(
val = sequence_to_be_sorted
off = middle_index
len = 1
with = buffer ).
 
sequence_to_be_sorted = replace(
val = sequence_to_be_sorted
off = low_index
len = 1
with = current_color ).
 
low_index = low_index + 1.
 
middle_index = middle_index + 1.
elseif current_color eq dutch_flag_colors-blue.
buffer = sequence_to_be_sorted+high_index(1).
 
sequence_to_be_sorted = replace(
val = sequence_to_be_sorted
off = middle_index
len = 1
with = buffer ).
 
sequence_to_be_sorted = replace(
val = sequence_to_be_sorted
off = high_index
len = 1
with = current_color ).
 
high_index = high_index - 1.
else.
middle_index = middle_index + 1.
endif.
endwhile.
endmethod.
 
 
method sorting_problem~is_sorted.
sorted = abap_true.
 
do strlen( sequence_to_check ) - 1 times.
data(current_character_index) = sy-index - 1.
data(current_color) = sequence_to_check+current_character_index(1).
data(next_color) = sequence_to_check+sy-index(1).
 
sorted = cond abap_bool(
when ( current_color eq dutch_flag_colors-red and
( next_color eq current_color or
next_color eq dutch_flag_colors-white or
next_color eq dutch_flag_colors-blue ) )
or
( current_color eq dutch_flag_colors-white and
( next_color eq current_color or
next_color eq dutch_flag_colors-blue ) )
or
( current_color eq dutch_flag_colors-blue and
current_color eq next_color )
then sorted
else abap_false ).
 
check sorted eq abap_false.
return.
enddo.
endmethod.
endclass.
 
 
start-of-selection.
data dutch_national_flag_problem type ref to sorting_problem.
 
dutch_national_flag_problem = new dutch_national_flag_problem( ).
 
data(sequence) = dutch_national_flag_problem->generate_unsorted_sequence( 20 ).
 
write:|{ sequence }, is sorted? -> { dutch_national_flag_problem->is_sorted( sequence ) }|, /.
 
dutch_national_flag_problem->sort_sequence( changing sequence_to_be_sorted = sequence ).
 
write:|{ sequence }, is sorted? -> { dutch_national_flag_problem->is_sorted( sequence ) }|, /.
</syntaxhighlight>
 
{{output}}
<pre>
RBWRWWRBWWRWBBRBRRWR, is sorted? ->
 
RRRRRRRRWWWWWWWBBBBB, is sorted? -> X
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
 
PROC PrintArray(BYTE ARRAY a BYTE len)
CHAR ARRAY colors(3)=['R 'W 'B]
BYTE i,index
 
FOR i=0 TO len-1
DO
index=a(i)
Put(colors(index))
OD
RETURN
 
BYTE FUNC IsSorted(BYTE ARRAY a BYTE len)
BYTE i
IF len<=1 THEN
RETURN (1)
FI
FOR i=0 TO len-2
DO
IF a(i)>a(i+1) THEN
RETURN (0)
FI
OD
RETURN (1)
 
PROC Randomize(BYTE ARRAY a BYTE len)
BYTE i
 
FOR i=0 TO len-1
DO
a(i)=Rand(3)
OD
RETURN
 
PROC Main()
DEFINE SIZE="30"
BYTE ARRAY a(SIZE)
 
Put(125) PutE() ;clear the screen
DO
Randomize(a,SIZE)
UNTIL IsSorted(a,SIZE)=0
OD
PrintE("Unsorted:") PrintArray(a,SIZE)
PutE() PutE()
 
SortB(a,SIZE,0)
PrintE("Sorted:") PrintArray(a,SIZE)
PutE() PutE()
 
IF IsSorted(a,SIZE) THEN
PrintE("Sorting is valid")
ELSE
PrintE("Sorting is invalid!")
FI
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Dutch_national_flag_problem.png Screenshot from Atari 8-bit computer]
<pre>
Unsorted:
RBWBWRRRRRWBWRWRRBBWBBBRRWWRRR
 
Sorted:
RRRRRRRRRRRRRRWWWWWWWWBBBBBBBB
 
Sorting is valid
</pre>
 
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Numerics.Discrete_Random, Ada.Command_Line;
 
procedure Dutch_National_Flag is
Line 113 ⟶ 389:
 
Print("After Sorting: ", A);
end Dutch_National_Flag;</langsyntaxhighlight>
 
{{out}}
Line 126 ⟶ 402:
Original Order: WHITE, WHITE, BLUE, WHITE, BLUE, BLUE, WHITE
After Sorting: WHITE, WHITE, WHITE, WHITE, BLUE, BLUE, BLUE</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # Dutch national flag problem: sort a set of randomly arranged red, white and blue balls into order #
# ball sets are represented by STRING items, red by "R", white by "W" and blue by "B" #
# returns the balls sorted into red, white and blue order #
PROC sort balls = ( STRING balls )STRING:
BEGIN
[ 1 : ( UPB balls + 1 ) - LWB balls ]CHAR result, white, blue;
INT r pos := 0, w pos := 0, b pos := 0;
# copy the red balls into the result and split the white and blue #
# into separate lists #
FOR pos FROM LWB balls TO UPB balls DO
CHAR b = balls[ pos ];
IF b = "R" THEN
# red ball - add to the result #
result[ r pos +:= 1 ] := b
ELIF b = "W" THEN
# white ball #
white[ w pos +:= 1 ] := b
ELSE
# must be blue #
blue[ b pos +:= 1 ] := b
FI
OD;
# add the white balls to the list #
IF w pos > 0 THEN
# there were some white balls - add them to the result #
result[ r pos + 1 : r pos + w pos ] := white[ 1 : w pos ];
r pos +:= w pos
FI;
# add the blue balls to the list #
IF b pos > 0 THEN
# there were some blue balls - add them to the end of the result #
result[ r pos + 1 : r pos + b pos ] := blue[ 1 : b pos ];
r pos +:= b pos
FI;
result[ 1 : r pos ]
END # sort balls # ;
# returns TRUE if balls is sorted, FALSE otherwise #
PROC sorted balls = ( STRING balls )BOOL:
BEGIN
BOOL result := TRUE;
FOR i FROM LWB balls + 1 TO UPB balls
WHILE result := ( CHAR prev = balls[ i - 1 ];
CHAR curr = balls[ i ];
prev = curr
OR ( prev = "R" AND curr = "W" )
OR ( prev = "R" AND curr = "B" )
OR ( prev = "W" AND curr = "B" )
)
DO SKIP OD;
result
END # sorted balls # ;
# constructs an unsorted random string of n balls #
PROC random balls = ( INT n )STRING:
BEGIN
STRING result := n * "?";
WHILE FOR i TO n DO
result[ i ] := IF INT r = ENTIER( next random * 3 ) + 1;
r = 1
THEN "R"
ELIF r = 2
THEN "W"
ELSE "B"
FI
OD;
sorted balls( result )
DO SKIP OD;
result
END # random balls # ;
# tests #
FOR i FROM 11 BY 3 TO 17 DO
STRING balls;
balls := random balls( i );
print( ( "before: ", balls, IF sorted balls( balls ) THEN " initially sorted??" ELSE "" FI, newline ) );
balls := sort balls( balls );
print( ( "after: ", balls, IF sorted balls( balls ) THEN "" ELSE " NOT" FI, " sorted", newline ) )
OD
END</syntaxhighlight>
{{out}}
<pre>
before: BWWRWRBWBRR
after: RRRRWWWWBBB sorted
before: BBRBWWRWBRRBBW
after: RRRRWWWWBBBBBB sorted
before: WRRWRRRBBWBRRWBWB
after: RRRRRRRWWWWWBBBBB sorted
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later.
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" --<www.macscripter.net/t/timsort-and-nigsort/71383/3>
 
on DutchNationalFlagProblem(numberOfBalls)
script o
property colours : {"red", "white", "blue"}
property balls : {}
-- Custom comparison handler for the sort.
on isGreater(a, b)
return ((a ≠ b) and ((a is "blue") or (b is "red")))
end isGreater
end script
repeat numberOfBalls times
set end of o's balls to some item of o's colours
end repeat
tell sorter to sort(o's balls, 1, numberOfBalls, {comparer:o})
return o's balls
end DutchNationalFlagProblem
 
DutchNationalFlagProblem(100)</syntaxhighlight>
 
{{output}}
<pre>Log:
(*blue, blue, blue, white, blue, white, white, white, red, blue, red, red, white, white, blue, white, blue, white, red, blue, white, white, blue, white, red, blue, white, white, white, blue, blue, red, blue, red, red, blue, white, white, red, white, red, red, red, blue, red, blue, red, blue, red, white, blue, white, red, white, red, white, white, blue, red, blue, blue, red, white, blue, red, white, blue, white, red, blue, white, white, white, blue, red, white, white, white, white, blue, red, red, white, red, red, red, white, white, red, blue, white, red, red, red, red, red, white, red, red, white*)
 
Result:
{"red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "white", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue"}</pre>
 
In the unlikely event of this being something you'll want done often at very high speeds, Dijkstra's own algorithm for the task is somewhat faster:
 
<syntaxhighlight lang="applescript">on threeWayPartition(theList, order) -- Dijkstra's algorithm.
script o
property lst : theList
end script
set {v1, v2, v3} to order
set {i, j, k} to {1, 1, (count o's lst)}
repeat until (j > k)
set this to o's lst's item j
if (this = v3) then
set o's lst's item j to o's lst's item k
set o's lst's item k to this
set k to k - 1
else
if (this = v1) then
set o's lst's item j to o's lst's item i
set o's lst's item i to this
set i to i + 1
end if
set j to j + 1
end if
end repeat
return -- Input list sorted in place.
end threeWayPartition
 
on DutchNationalFlagProblem(numberOfBalls)
script o
property balls : {}
end script
set colours to {"red", "white", "blue"}
repeat numberOfBalls times
set end of o's balls to some item of colours
end repeat
threeWayPartition(o's balls, colours)
return o's balls
end DutchNationalFlagProblem
 
DutchNationalFlagProblem(100)</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
{{trans|ZX_Spectrum_Basic}}
<syntaxhighlight lang="applesoftbasic"> 100 READ C$(0),C$(1),C$(2)
110 DATARED,WHITE,BLUE,0
120 PRINT "RANDOM:
130 FOR N = 0 TO 9
140 LET B%(N) = RND (1) * 3
150 GOSUB 250
160 NEXT N
170 PRINT
180 READ S
190 PRINT "SORTED:
200 FOR I = 0 TO 2
210 FOR N = 0 TO 9
220 ON B%(N) = I GOSUB 250
230 NEXT N,I
240 END
250 PRINT SPC( S)C$(B%(N));
260 LET S = 1
270 RETURN </syntaxhighlight>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">RandGen(MaxBalls){
Random,k,3,MaxBalls
Loop,% k{
Random,k,1,3
o.=k
}return o
}
While((!InStr(o,1)||!InStr(o,2)||!InStr(o,3))||!RegExReplace(o,"\b1+2+3+\b"))
o:=RandGen(3)
Loop,% StrLen(o)
F.=SubStr(o,A_Index,1) ","
F:=RTrim(F,",")
Sort,F,N D`,
MsgBox,% F:=RegExReplace(RegExReplace(RegExReplace(F,"(1)","Red"),"(2)","White"),"(3)","Blue")</syntaxhighlight>
 
=={{header|AutoIt}}==
Given each color a value in descending order ( Red = 1, White = 2 And Blue = 3)
<syntaxhighlight lang="autoit">
<lang Autoit>
#include <Array.au3>
Dutch_Flag(50)
Line 154 ⟶ 631:
_ArrayDisplay($avArray)
EndFunc ;==>Dutch_Flag
</syntaxhighlight>
</lang>
 
=={{header|BBC BASICAWK}}==
{{works with|gawk}}
<syntaxhighlight lang="awk">
BEGIN {
weight[1] = "red"; weight[2] = "white"; weight[3] = "blue";
# ballnr must be >= 3. Using very high numbers here may make your computer
# run out of RAM. (10 millions balls ~= 2.5GiB RAM on x86_64)
ballnr = 10
 
srand()
# Generating a random pool of balls. This python-like loop is actually
# a prettyfied one-liner
do
for (i = 1; i <= ballnr; i++)
do
balls[i] = int(3 * rand() + 1)
# These conditions ensure the 3 first balls contains
# a white, blue and red ball. Removing 'i < 4' would
# hit performance a lot.
while ( (i < 4 && i > 1 && balls[i] == balls[i - 1]) ||
(i < 4 && i > 2 && balls[i] == balls[i - 2]) )
while (is_dnf(balls, ballnr))
printf("BEFORE: ")
print_balls(balls, ballnr, weight)
# Using gawk default quicksort. Using variants of PROCINFO["sorted_in"]
# wasn't faster than a simple call to asort().
asort(balls)
 
printf("\n\nAFTER : ")
print_balls(balls, ballnr, weight)
sorting = is_dnf(balls, ballnr) ? "valid" : "invalid"
print("\n\nSorting is " sorting ".")
}
 
function print_balls(balls, ballnr, weight ,i) {
for (i = 1; i <= ballnr; i++)
printf("%-7s", weight[balls[i]])
}
 
function is_dnf(balls, ballnr) {
# Checking if the balls are sorted in the Dutch national flag order,
# using a simple scan with weight comparison
for (i = 2; i <= ballnr; i++)
if (balls[i - 1] > balls[i])
return 0
return 1
}
</syntaxhighlight>
 
Output:
 
<syntaxhighlight lang="text">
BEFORE: blue red white red white blue red white blue white
 
AFTER : red red red white white white white blue blue blue
 
Sorting is valid.
</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|BaCon}}===
<syntaxhighlight lang="qbasic">DECLARE color$[] = { "red", "white", "blue" }
 
DOTIMES 16
ball$ = APPEND$(ball$, 0, color$[RANDOM(3)] )
DONE
 
PRINT "Unsorted: ", ball$
 
PRINT " Sorted: ", REPLACE$(SORT$(REPLACE$(ball$, "blue", "z")), "z", "blue")</syntaxhighlight>
{{out}}
<pre>
Unsorted: red white blue blue red white white white blue white blue red blue red white red
Sorted: red red red red red white white white white white white blue blue blue blue blue
</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">arraybase 1
dim flag = {"Red","White","Blue"}
dim balls(9)
 
print "Random: |";
for i = 1 to 9
kolor = (rand * 3) + 1
balls[i] = flag[kolor]
print balls[i]; " |";
next i
print
 
print "Sorted: |";
for i = 1 to 3
kolor = flag[i]
for j = 1 to 9
if balls[j] = kolor then print balls[j]; " |";
next j
next i</syntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(0,0)
Line 195 ⟶ 772:
prev% = weight%
NEXT
IF NOT sorted% PRINT "Error: Balls are not in correct order!"</langsyntaxhighlight>
{{out}}
'''Output:'''
<pre>
Random list: Red White Red Blue White Red White Blue Red Red Blue Red
Line 203 ⟶ 780:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h> //printf()
#include <stdlib.h> //srand(), rand(), RAND_MAX, qsort()
#include <stdbool.h> //true, false
Line 210 ⟶ 787:
#define NUMBALLS 5 //NUMBALLS>1
 
int compar(const void *a, const void *b){
char c1=*(const char*)a, c2=*(const char*)b; //first cast void* to char*, then dereference
return c1-c2;
}
Line 256 ⟶ 833:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Accidentally still sorted:rrrww
Line 263 ⟶ 840:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
 
Line 303 ⟶ 880:
dnf_partition(balls, balls + 9, WHITE, BLUE);
print(balls, 9);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 310 ⟶ 887:
Balls: red red red white white white blue blue blue
Sorted: true
</pre>
 
=={{header|C_sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace RosettaCode
{
class Program
{
static void QuickSort(IComparable[] elements, int left, int right)
{
int i = left, j = right;
IComparable pivot = elements[left + (right - left) / 2];
 
while (i <= j)
{
while (elements[i].CompareTo(pivot) < 0) i++;
while (elements[j].CompareTo(pivot) > 0) j--;
 
if (i <= j)
{
// Swap
IComparable tmp = elements[i];
elements[i] = elements[j];
elements[j] = tmp;
i++;
j--;
}
}
 
// Recursive calls
if (left < j) QuickSort(elements, left, j);
if (i < right) QuickSort(elements, i, right);
}
const int NUMBALLS = 5;
static void Main(string[] args)
{
Func<string[], bool> IsSorted = (ballList) =>
{
int state = 0;
for (int i = 0; i < NUMBALLS; i++)
{
if (int.Parse(ballList[i]) < state)
return false;
if (int.Parse(ballList[i]) > state)
state = int.Parse(ballList[i]);
}
return true;
};
Func<string[], string> PrintOut = (ballList2) =>
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < NUMBALLS; i++)
str.Append(int.Parse(ballList2[i]) == 0 ? "r" : int.Parse(ballList2[i]) == 1 ? "w" : "b");
return str.ToString();
};
bool continueLoop = true;
string[] balls = new string[NUMBALLS]; // 0 = r, 1 = w, 2 = b
Random numberGenerator = new Random();
do // Enforce that we start with non-sorted balls
{
// Generate balls
for (int i = 0; i < NUMBALLS; i++)
balls[i] = numberGenerator.Next(3).ToString();
 
continueLoop = IsSorted(balls);
if (continueLoop)
Console.WriteLine("Accidentally still sorted: {0}", PrintOut(balls));
} while (continueLoop);
Console.WriteLine("Non-sorted: {0}", PrintOut(balls));
QuickSort(balls, 0, NUMBALLS - 1); // Sort them using quicksort
Console.WriteLine("{0}: {1}", IsSorted(balls) ? "Sorted" : "Sort failed", PrintOut(balls));
}
}
}
</syntaxhighlight>
 
=={{header|Ceylon}}==
Be sure to add ceylon.random in your module.ceylon file.
<syntaxhighlight lang="ceylon">import ceylon.random {
 
DefaultRandom
}
 
abstract class Colour(name, ordinal) of red | white | blue satisfies Comparable<Colour> {
shared String name;
shared Integer ordinal;
string => name;
compare(Colour other) => this.ordinal <=> other.ordinal;
}
 
object red extends Colour("red", 0) {}
object white extends Colour("white", 1) {}
object blue extends Colour("blue", 2) {}
 
Colour[] allColours = `Colour`.caseValues;
 
shared void run() {
function ordered({Colour*} colours) =>
colours.paired.every(([c1, c2]) => c1 <= c2);
value random = DefaultRandom();
function randomBalls(Integer length = 15) {
while (true) {
value balls = random.elements(allColours).take(length);
if (!ordered(balls)) {
return balls.sequence();
}
}
}
function dutchSort({Colour*} balls, Colour mid = white) {
value array = Array { *balls };
if (array.empty) {
return [];
}
variable value i = 0;
variable value j = 0;
variable value n = array.size - 1;
while (j <= n) {
assert (exists ball = array[j]);
if (ball < mid) {
array.swap(i, j);
i ++;
j ++;
}
else if (ball > mid) {
array.swap(n, j);
n --;
}
else {
j ++;
}
}
return array;
}
function idiomaticSort({Colour*} balls) =>
balls.sort(increasing);
value initialBalls = randomBalls();
"the initial balls are not randomized"
assert (!ordered(initialBalls));
 
print(initialBalls);
value sortedBalls1 = idiomaticSort(initialBalls);
value sortedBalls2 = dutchSort(initialBalls);
"the idiomatic sort didn't work"
assert (ordered(sortedBalls1));
"the dutch sort didn't work"
assert (ordered(sortedBalls2));
print(sortedBalls1);
print(sortedBalls2);
}</syntaxhighlight>
 
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(defn dutch-flag-order [color]
(get {:red 1 :white 2 :blue 3} color))
 
(defn sort-in-dutch-flag-order [balls]
(sort-by dutch-flag-order balls))
 
;; Get a collection of 'n' balls of Dutch-flag colors
(defn random-balls [num-balls]
(repeatedly num-balls
#(rand-nth [:red :white :blue])))
 
;; Get random set of balls and insure they're not accidentally sorted
(defn starting-balls [num-balls]
(let [balls (random-balls num-balls)
in-dutch-flag-order? (= balls
(sort-in-dutch-flag-order balls))]
(if in-dutch-flag-order?
(recur num-balls)
balls)))</syntaxhighlight>
 
{{out}}
<pre>
(def balls (starting-balls 20))
balls ; (:blue :red :red :blue :white :blue :white :blue :white :red
; :blue :red :white :white :white :red :blue :white :blue :blue)
 
(sort-in-dutch-flag-order balls) ; (:red :red :red :red :red :white :white :white :white :white
; :white :white :blue :blue :blue :blue :blue :blue :blue :blue)
</pre>
 
=={{header|Common Lisp}}==
{{trans|Clojure}}
<syntaxhighlight lang="lisp">
(defun dutch-flag-order (color)
(case color (:red 1) (:white 2) (:blue 3)))
 
(defun sort-in-dutch-flag-order (balls)
(sort (copy-list balls) #'< :key #'dutch-flag-order))
 
(defun make-random-balls (count)
(loop :repeat count
:collect (nth (random 3) '(:red :white :blue))))
 
(defun make-balls (count)
(loop :for balls = (make-random-balls count)
:while (equal balls (sort-in-dutch-flag-order balls))
:finally (return balls)))
 
;; Alternative version showcasing iterate's finding clause
(defun make-balls2 (count)
(iter (for balls = (make-random-balls count))
(finding balls such-that (not (equal balls (sort-in-dutch-flag-order balls))))))
</syntaxhighlight>
{{out}}
<pre>
CL-USER> (defvar *balls* (make-balls 20))
*BALLS*
CL-USER> *balls*
(:WHITE :WHITE :WHITE :WHITE :RED :BLUE :RED :RED :WHITE :WHITE :RED :BLUE :RED
:RED :BLUE :WHITE :BLUE :BLUE :BLUE :BLUE)
CL-USER> (sort-in-dutch-flag-order *balls*)
(:RED :RED :RED :RED :RED :RED :WHITE :WHITE :WHITE :WHITE :WHITE :WHITE :WHITE
:BLUE :BLUE :BLUE :BLUE :BLUE :BLUE :BLUE)
</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.algorithm, std.traits, std.array;
 
enum DutchColors { red, white, blue }
 
void dutchNationalFlagSort(DutchColors[] items) pure nothrow @nogc {
int lo, mid, hi = items.length - 1;
 
Line 337 ⟶ 1,144:
DutchColors[12] balls;
foreach (ref ball; balls)
ball = [EnumMembersuniform!DutchColors][uniform(0, $)];
 
writeln("Original Ball order:\n", balls);
balls.dutchNationalFlagSort();
writeln("\nSorted Ball Order:\n", balls);
assert(balls[].isSorted(), "Balls not sorted.");
}</langsyntaxhighlight>
{{out}}
<pre>Original Ball order:
Line 352 ⟶ 1,159:
 
===Bidirectional Range Version===
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.algorithm, std.range,
std.array, std.traits;
 
Line 423 ⟶ 1,230:
assert(balls[0 .. n].isSorted());
}
}</langsyntaxhighlight>
The output is the same.
 
===More Verified Version===
This version uses more contract programming and asserts to verify the code correctness.
With hints from: toccata.lri.fr/gallery/flag.en.html
<syntaxhighlight lang="d">import std.stdio, std.random, std.algorithm, std.traits, std.range;
 
enum Color : ubyte { blue, white, red }
 
immutable isMonochrome = (in Color[] a, in size_t i, in size_t j, in Color c)
pure nothrow @safe @nogc => iota(i, j).all!(k => a[k] == c);
 
bool isPermutation(in Color[] a1, in Color[] a2) pure nothrow @safe @nogc {
size_t[EnumMembers!Color.length] counts1, counts2;
foreach (immutable x; a1)
counts1[x]++;
foreach (immutable x; a2)
counts2[x]++;
return counts1 == counts2;
}
 
 
void dutchNationalFlagSort(Color[] a) pure nothrow @safe @nogc
// This function is not @nogc in -debug builds.
/*
Scan of the array 'a' from left to right using 'i' and we
maintain this invariant, using indices 'b' and 'r':
 
0 b i r
+---------+----------+-----------+-------+
| blue | white | ? | red |
+---------+----------+-----------+-------+
*/
out {
// Find b and r.
immutable bRaw = a.countUntil!q{a != b}(Color.blue);
immutable size_t b = (bRaw == -1) ? a.length : bRaw;
immutable rRaw = a.retro.countUntil!q{a != b}(Color.red);
immutable size_t r = (rRaw == -1) ? 0 : (a.length - rRaw);
 
assert(isMonochrome(a, 0, b, Color.blue));
assert(isMonochrome(a, b, r, Color.white));
assert(isMonochrome(a, r, a.length, Color.red));
// debug assert(isPermutation(a, a.old));
} body {
size_t b = 0, i = 0, r = a.length;
debug {
/*ghost*/ immutable aInit = a.idup; // For loop invariant.
/*ghost*/ size_t riPred = r - i; // For loop variant.
}
 
while (i < r) {
/*invariant*/ assert(0 <= b && b <= i && i <= r && r <= a.length);
/*invariant*/ assert(isMonochrome(a, 0, b, Color.blue));
/*invariant*/ assert(isMonochrome(a, b, i, Color.white));
/*invariant*/ assert(isMonochrome(a, r, a.length, Color.red));
/*invariant*/ debug assert(isPermutation(a, aInit));
 
final switch (a[i]) with (Color) {
case blue:
a[b].swap(a[i]);
b++;
i++;
break;
case white:
i++;
break;
case red:
r--;
a[r].swap(a[i]);
break;
}
 
debug {
/*variant*/ assert((r - i) < riPred);
riPred = r - i;
}
}
}
 
void main() {
Color[12] balls;
 
// Test special cases.
foreach (immutable color; [EnumMembers!Color]) {
balls[] = color;
balls.dutchNationalFlagSort;
assert(balls[].isSorted, "Balls not sorted.");
}
 
foreach (ref b; balls)
b = uniform!Color;
 
writeln("Original Ball order:\n", balls);
balls.dutchNationalFlagSort;
writeln("\nSorted Ball Order:\n", balls);
assert(balls[].isSorted, "Balls not sorted.");
}</syntaxhighlight>
The output is the same.
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
Encodes the colors to strings of "1" "2" and "3" to allow them to be sorted. Then it sses Delphi TStringList to sort the colors.
 
<syntaxhighlight lang="Delphi">
 
const TestOrder: array [0..11] of string =
('Blue','Blue','White','Blue','White','Blue',
'Red','White','White','Blue','White','Red');
 
 
procedure DoDutchFlag(Memo: TMemo; Order: array of string);
{Solve dutch flag color order using TStringList component}
{Encode colors "Red", "White" and "Blue" to "1", "2", and "3" }
{This allows them to be sorted in the TString List}
var I: integer;
var SL: TStringList;
var S2: string;
 
function DecodeList(SL: TStringList): string;
{Convert encoded colors 1, 2 and 3 to Red, White and Blue}
var I: integer;
begin
Result:='';
for I:=0 to SL.Count-1 do
begin
if I>0 then Result:=Result+',';
if SL[I]='1' then Result:=Result+'Red'
else if SL[I]='2' then Result:=Result+'White'
else Result:=Result+'Blue'
end;
end;
 
begin
SL:=TStringList.Create;
try
{Encode colors from array of strings}
for I:=0 to High(TestOrder) do
begin
if Order[I]='Red' then SL.Add('1')
else if Order[I]='White' then SL.Add('2')
else SL.Add('3');
end;
Memo.Lines.Add('Original Order:');
Memo.Lines.Add('['+DecodeList(SL)+']');
SL.Sort;
Memo.Lines.Add('Original Order:');
Memo.Lines.Add('['+DecodeList(SL)+']');
finally SL.Free; end;
end;
 
 
procedure ShowDutchFlag(Memo: TMemo);
begin
DoDutchFlag(Memo,TestOrder);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Original Order:
[Blue,Blue,White,Blue,White,Blue,Red,White,White,Blue,White,Red]
Original Order:
[Red,Red,White,White,White,White,White,Blue,Blue,Blue,Blue,Blue]
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
col$[] = [ "red" "white" "blue" ]
for i to 8
b[] &= randint 3
.
for b in b[]
write col$[b] & " "
if b < b0
not_sorted = 1
.
b0 = b
.
print ""
print ""
if not_sorted = 0
print "already sorted"
else
for i = 1 to len b[] - 1
for j = i + 1 to len b[]
if b[j] < b[i]
swap b[j] b[i]
.
.
.
for b in b[]
write col$[b] & " "
.
.
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Erlang}}
<syntaxhighlight lang="elixir">defmodule Dutch_national_flag do
defp ball(:red), do: 1
defp ball(:white), do: 2
defp ball(:blue), do: 3
defp random_ball, do: Enum.random([:red, :white, :blue])
defp random_ball(n), do: (for _ <- 1..n, do: random_ball())
defp is_dutch([]), do: true
defp is_dutch([_]), do: true
defp is_dutch([b,h|l]), do: ball(b) < ball(h) and is_dutch([h|l])
defp is_dutch(_), do: false
def dutch(list), do: dutch([], [], [], list)
defp dutch(r, w, b, []), do: r ++ w ++ b
defp dutch(r, w, b, [:red | list]), do: dutch([:red | r], w, b, list)
defp dutch(r, w, b, [:white | list]), do: dutch(r, [:white | w], b, list)
defp dutch(r, w, b, [:blue | list]), do: dutch(r, w, [:blue | b], list)
def problem(n \\ 10) do
list = random_ball(n)
if is_dutch(list) do
IO.puts "The random sequence #{inspect list} is already in the order of the Dutch flag!"
else
IO.puts "The starting random sequence is #{inspect list};"
IO.puts "The ordered sequence is #{inspect dutch(list)}."
end
end
end
 
Dutch_national_flag.problem</syntaxhighlight>
 
{{out}}
<pre>
The starting random sequence is [:blue, :white, :blue, :red, :red, :white, :blue
, :white, :white, :blue];
The ordered sequence is [:red, :red, :white, :white, :white, :white, :blue, :blu
e, :blue, :blue].
</pre>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">-module(dutch).
-export([random_balls/1, is_dutch/1, dutch/1]).
 
Line 452 ⟶ 1,500:
dutch(R, W, B, [red | L]) -> dutch([red|R], W, B, L);
dutch(R, W, B, [white | L]) -> dutch(R, [white|W], B, L);
dutch(R, W, B, [blue | L]) -> dutch(R, W, [blue|B], L).</langsyntaxhighlight>
 
Sample usage:
<langsyntaxhighlight lang="erlang">main(_) ->
L = random_balls(10),
case is_dutch(L) of
true -> io:format("The random sequence ~p is already in the order of the Dutch flag!~n", [L]);
false -> io:format("The starting random sequence is ~p;~nThe ordered sequence is ~p.~n", [L, dutch(L)])
end.</langsyntaxhighlight>
 
{{out}}
Sample output:
<pre>The starting random sequence is [white,white,blue,blue,white,red,white,blue,
blue,white];
The ordered sequence is [red,white,white,white,white,white,blue,blue,blue,
blue].</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">(* Since the task description here does not impose Dijsktra's original restrictions
* Changing the order is only allowed by swapping 2 elements
* Every element must only be inspected once
we have several options ...
One way -- especially when we work with immutable data structures --
is to scan the unordered list, collect the different
colours on our way and append the 3 sub-lists in the correct order.
*)
let rnd = System.Random()
 
type color = | Red | White | Blue
 
let isDutch s =
Seq.forall2 (fun last this ->
match (last, this) with
| (Red, Red) | (Red, White) | (White, White) | (White, Blue) | (Blue, Blue) -> true | _ -> false
) s (Seq.skip 1 s)
 
[<EntryPoint>]
let main argv =
let n = 10
let rec getBallsToSort n s =
let sn = Seq.take n s
if (isDutch sn) then (getBallsToSort n (Seq.skip 1 s)) else sn
let balls = getBallsToSort n (Seq.initInfinite (fun _ -> match (rnd.Next(3)) with | 0 -> Red | 1 -> White | _ -> Blue))
printfn "Sort the sequence of %i balls: %A" n (Seq.toList balls)
let (rs,ws,bs) =
balls
|> Seq.fold (fun (rs,ws,bs) b ->
match b with | Red -> (b::rs,ws,bs) | White -> (rs,b::ws,bs) | Blue -> (rs,ws,b::bs))
([],[],[])
let sorted = rs @ ws @ bs
printfn "The sequence %A is sorted: %b" sorted (isDutch sorted)
0</syntaxhighlight>
{{out}}
<pre>Sort the sequence of 10 balls: [Red; White; Red; Blue; White; White; Blue; Blue; White; White]
The sequence [Red; Red; White; White; White; White; White; Blue; Blue; Blue] is sorted: true</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<syntaxhighlight lang="factor">USING: combinators grouping kernel math prettyprint random
sequences ;
 
: sorted? ( seq -- ? ) [ <= ] monotonic? ;
 
: random-non-sorted-integers ( length n -- seq )
2dup random-integers
[ dup sorted? ] [ drop 2dup random-integers ] while 2nip ;
 
: dnf-sort! ( seq -- seq' )
[ 0 0 ] dip [ length 1 - ] [ ] bi
[ 2over <= ] [
pick over nth {
{ 0 [ reach reach pick exchange [ [ 1 + ] bi@ ] 2dip ] }
{ 1 [ [ 1 + ] 2dip ] }
[ drop 3dup exchange [ 1 - ] dip ]
} case
] while 3nip ;
 
10 3 random-non-sorted-integers dup . dnf-sort! .</syntaxhighlight>
{{out}}
<pre>
{ 1 2 2 0 0 0 1 0 0 1 }
{ 0 0 0 0 0 1 1 1 2 2 }
</pre>
 
=={{header|Forth}}==
This demo is by no means exemplary however there was no other Forth entry. This code runs on the infamous TI-99, one of the slowest computers ever. This demo uses Dijkstra's three colour algorithm to sort four different inputs. The flag is sorted on the screen so you can see it happen. The input data patterns are: random, checker-board, Russian flag and French (imperfect) flag.
Using three variables for the screen position pointers (vs stack juggling) makes the Dijkstra algorithm translate nicely into Forth.
 
A video of the results can be seen here:
 
https://github.com/bfox9900/CAMEL99-V2/blob/master/Video/DIJKSTRAFLAG%20.mp4
 
<syntaxhighlight lang="text">\ Dutch flag DEMO for CAMEL99 Forth
\ *SORTS IN PLACE FROM Video MEMORY*
 
INCLUDE DSK1.GRAFIX.F
INCLUDE DSK1.RANDOM.F
INCLUDE DSK1.CASE.F
 
\ TMS9918 Video chip Specific code
HEX
FFFF FFFF FFFF FFFF PATTERN: SQUARE
 
\ define colors and characters
DECIMAL
24 32 * CONSTANT SIZE \ flag will fill GRAPHICS screen
SIZE 3 / CONSTANT #256 \ 256 chars per segment of flag
1 CONSTANT REDSQR \ red character
9 CONSTANT WHTSQR \ white character
19 CONSTANT BLUSQR \ blue character
 
\ color constants
1 CONSTANT TRANS
7 CONSTANT RED
5 CONSTANT BLU
16 CONSTANT WHT
 
SQUARE REDSQR CHARDEF
SQUARE BLUSQR CHARDEF
SQUARE WHTSQR CHARDEF
 
\ charset FG BG
0 RED TRANS COLOR
1 WHT TRANS COLOR
2 BLU TRANS COLOR
 
\ screen fillers
: RNDI ( -- n ) SIZE 1+ RND ; \ return a random VDP screen address
 
: NOTRED ( -- n ) \ return rnd index that is not RED
BEGIN
RNDI DUP VC@ REDSQR =
WHILE DROP
REPEAT ;
 
: NOTREDWHT ( -- n ) \ return rnd index that is not RED or WHITE
BEGIN RNDI DUP
VC@ DUP REDSQR =
SWAP WHTSQR = OR
WHILE
DROP
REPEAT ;
 
: RNDRED ( -- ) \ Random RED on VDP screen
#256 0 DO REDSQR NOTRED VC! LOOP ;
 
: RNDWHT ( -- ) \ place white where there is no red or white
#256 0 DO WHTSQR NOTREDWHT VC! LOOP ;
 
: BLUSCREEN ( -- )
0 768 BLUSQR VFILL ;
 
\ load the screen with random red,white&blue squares
: RNDSCREEN ( -- )
BLUSCREEN RNDRED RNDWHT ;
 
: CHECKERED ( -- ) \ red,wht,blue checker board
SIZE 0
DO
BLUSQR I VC!
WHTSQR I 1+ VC!
REDSQR I 2+ VC!
3 +LOOP ;
 
: RUSSIAN \ Russian flag
0 0 WHTSQR 256 HCHAR
0 8 BLUSQR 256 HCHAR
0 16 REDSQR 256 HCHAR ;
 
: FRENCH \ kind of a French flag
0 0 BLUSQR 256 VCHAR
10 16 WHTSQR 256 VCHAR
21 8 REDSQR 256 VCHAR ;
 
\ =======================================================
\ Algorithm Dijkstra(A) \ A is an array of three colors
\ begin
\ r <- 1;
\ b <- n;
\ w <- n;
\ while (w>=r)
\ check the color of A[w]
\ case 1: red
\ swap(A[r],A [w]);
\ r<-r+1;
\ case 2: white
\ w<-w-1
\ case 3: blue
\ swap(A[w],A[b]);
\ w<-w-1;
\ b<-b-1;
\ end
 
\ ======================================================
\ Dijkstra three color Algorithm in Forth
 
\ screen address pointers
VARIABLE R
VARIABLE B
VARIABLE W
 
: XCHG ( vadr1 vadr2 -- ) \ Exchange chars in Video RAM
OVER VC@ OVER VC@ ( -- addr1 addr2 char1 char2)
SWAP ROT VC! SWAP VC! ; \ exchange chars in Video RAM
 
: DIJKSTRA ( -- )
0 R !
SIZE 1- DUP B ! W !
BEGIN
W @ R @ 1- >
WHILE
W @ VC@ ( fetch Video char at pointer W)
CASE
REDSQR OF R @ W @ XCHG
1 R +! ENDOF
 
WHTSQR OF -1 W +! ENDOF
 
BLUSQR OF W @ B @ XCHG
-1 W +!
-1 B +! ENDOF
ENDCASE
REPEAT ;
 
: WAIT ( -- ) 11 11 AT-XY ." Finished!" 1500 MS ;
 
: RUN ( -- )
PAGE
CR ." Dijkstra Dutch flag Demo" CR
CR ." Sorted in-place in Video RAM" CR
CR
CR ." Using the 3 colour algorithm" CR
CR ." Press any key to begin" KEY DROP
RNDSCREEN DIJKSTRA WAIT
CHECKERED DIJKSTRA WAIT
RUSSIAN DIJKSTRA WAIT
FRENCH DIJKSTRA WAIT
0 23 AT-XY
CR ." Completed"
;
</syntaxhighlight>
 
=={{header|Fortran}}==
Please find the example run along with compilation instructions on a GNU/linux platform in the comments at the beginning of the FORTRAN 2008 program source. The Netherlands program, using equal numbers of colors, solved the problem at three sample sizes. Swaps number 2/3 the total of samples, convincingly demonstrating the O(n) time behavior that's directly provable by inspection. The color strings are chosen for ASCII sort. Feature not used.
 
Abhor code duplication. I've repeated code anyway to demonstrate FORTRAN pointers, which behave like an alias. A subroutine with traditional arguments including the number of valid elements of the array is appropriate. I'd use one long array instead of 3 arrays and the size intrinsic.
<syntaxhighlight lang="text">
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Mon Jun 3 11:18:24
!
!a=./f && make FFLAGS='-O0 -g' $a && OMP_NUM_THREADS=2 $a < unixdict.txt
!gfortran -std=f2008 -O0 -g -Wall -fopenmp -ffree-form -fall-intrinsics -fimplicit-none f.f08 -o f
! Original and flag sequences
! WHITE RED blue blue RED WHITE WHITE WHITE blue RED RED blue
! RED RED RED RED WHITE WHITE WHITE WHITE blue blue blue blue
! 12 items, 8 swaps.
! 999 items, 666 swaps.
! 9999 items, 6666 swaps.
!
!Compilation finished at Mon Jun 3 11:18:24
 
program Netherlands
 
character(len=6), parameter, dimension(3) :: colors = (/'RED ', 'WHITE ', 'blue '/)
integer, dimension(12) :: sort_me
integer, dimension(999), target :: a999
integer, dimension(9999), target :: a9999
integer, dimension(:), pointer :: pi
integer :: i, swaps
data sort_me/4*1,4*2,4*3/
call shuffle(sort_me, 5)
write(6,*)'Original and flag sequences'
write(6,*) (colors(sort_me(i)), i = 1, size(sort_me))
call partition3way(sort_me, 2, swaps)
write(6,*) (colors(sort_me(i)), i = 1, size(sort_me))
write(6,*) 12,'items,',swaps,' swaps.'
pi => a999
do i=1, size(pi)
pi(i) = 1 + L(size(pi)/3 .lt. i) + L(2*size(pi)/3 .lt. i)
end do
call shuffle(pi, size(pi)/3+1)
call partition3way(pi, 2, swaps)
write(6,*) size(pi),'items,',swaps,' swaps.'
pi => a9999
do i=1, size(pi)
pi(i) = 1 + L(size(pi)/3 .lt. i) + L(2*size(pi)/3 .lt. i)
end do
call shuffle(pi, size(pi)/3+1)
call partition3way(pi, 2, swaps)
write(6,*) size(pi),'items,',swaps,' swaps.'
 
contains
 
integer function L(q)
! In Ken Iverson's spirit, APL logicals are more useful as integers.
logical, intent(in) :: q
if (q) then
L = 1
else
L = 0
end if
end function L
 
subroutine swap(a,i,j)
integer, dimension(:), intent(inout) :: a
integer, intent(in) :: i, j
integer :: t
t = a(i)
a(i) = a(j)
a(j) = t
end subroutine swap
 
subroutine partition3way(a, pivot, swaps)
integer, dimension(:), intent(inout) :: a
integer, intent(in) :: pivot
integer, intent(out) :: swaps
integer :: i, j, k
swaps = 0
i = 0
j = 1
k = size(a) + 1
do while (j .lt. k)
if (pivot .eq. a(j)) then
j = j+1
swaps = swaps-1
else if (pivot .lt. a(j)) then
k = k-1
call swap(a, k, j)
else
i = i+1
call swap(a, i, j)
j = j+1
end if
swaps = swaps+1
end do
end subroutine partition3way
 
subroutine shuffle(a, n) ! a rather specialized shuffle not for general use
integer, intent(inout), dimension(:) :: a
integer, intent(in) :: n
integer :: i, j, k
real :: harvest
do i=1, size(a)-1
call random_number(harvest)
harvest = harvest - epsilon(harvest)*L(harvest.eq.1)
k = L(i.eq.1)*(n-1) + i
j = i + int((size(a) - k) * harvest)
call swap(a, i, j)
end do
end subroutine shuffle
 
end program Netherlands
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
' El problema planteado por Edsger Dijkstra es:
' "Dado un número de bolas rojas, azules y blancas en orden aleatorio,
' ordénelas en el orden de los colores de la bandera nacional holandesa."
 
Dim As String c = "RBW", n = "121509"
Dim As Integer bolanum = 9
Dim As Integer d(bolanum), k, i, j
Randomize Timer
 
Color 15: Print "Aleatorio: ";
For k = 1 To bolanum
d(k) = Int(Rnd * 3) + 1
Color Val(Mid(n, d(k), 2))
Print Mid(c, d(k), 1) & Chr(219);
Next k
 
Color 15: Print : Print "Ordenado: ";
For i = 1 To 3
For j = 1 To bolanum
If d(j) = i Then Color Val(Mid(n, i, 2)): Print Mid(c, i, 1) & Chr(219);
Next j
Next i
End
</syntaxhighlight>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=e57a862aff12647fa80c84a595161cb9 Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim Red As String = "0"
Dim White As String = "1"
Dim Blue As String = "2"
Dim siCount As Short
Dim sColours As New String[]
Dim sTemp As String
 
For siCount = 1 To 20
sColours.Add(Rand(Red, Blue))
Next
 
Print "Random: - ";
 
For siCount = 1 To 2
For Each sTemp In sColours
If sTemp = Red Then Print "Red ";
If sTemp = White Then Print "White ";
If sTemp = Blue Then Print "Blue ";
Next
sColours.Sort
Print
If siCount = 1 Then Print "Sorted: - ";
Next
 
End</syntaxhighlight>
Output:
<pre>
Random: - Blue Red Red White White White White Red Blue White Red Red White Blue White White Blue Red White Blue
Sorted: - Red Red Red Red Red Red White White White White White White White White White Blue Blue Blue Blue Blue
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 555 ⟶ 2,002:
f.sort3()
fmt.Println(f)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 572 ⟶ 2,019:
The function "sort" works with anything that belongs to the Eq and Ord classes.
The function "randomRIO" takes a range of two integers to give a random value within the range. We make Color an instance of Enum so that we can give Red, White and Blue as integers to randomRIO and convert the random number back to Red, White or Blue.
<langsyntaxhighlight Haskelllang="haskell">import Data.List (sort)
import System.Random (randomRIO)
import System.IO.Unsafe (unsafePerformIO)
Line 597 ⟶ 2,044:
False -> do
putStrLn $ "The starting random sequence is " ++ show a ++ "\n"
putStrLn $ "The ordered sequence is " ++ show (dutch a)</langsyntaxhighlight>
{{out}}
Output:
<pre>
The starting random sequence is [White,Blue,Blue,Blue,Blue,Blue,Blue,Red,Red,
Line 608 ⟶ 2,055:
 
To understand ''why'' Dijsktra was interested in the problem, here's an example showing difficiency of using generic sort:
<syntaxhighlight lang="haskell">inorder n = and $ zipWith (<=) n (tail n) -- or use Data.List.Ordered
<lang haskell>import Data.List (sort)
 
inorder n = and $ zipWith (<=) n (tail n) -- or use Data.List.Ordered
 
mk012 :: Int -> Int -> [Int] -- definitely unordered
mk012 n = (++[0]).(2:).map (`mod` 3).take n.frr where
-- frr = Fast Rubbish Randoms
frr n = r:frrtail r. whereiterate r(\n =-> n * 7 + 13)
 
dutch1 n = (filter (==0) n)++(filter (==1) n)++(filter (==2) n)
 
dutch2 n = tric [] [] [] na++b++c where -- scan list once; it *may* help
(a,b,c) = foldl f ([],[],[]) n -- scan list once; it *may* help
tric a b c [] = a++b++c
tricf (a ,b ,c) (x:xs) = case x of
0 -> tric (x0:a), b, c xs)
1 -> tric (a, (x:b), c xs)
2 -> tric (a, b, (x:c) xs
 
main = do -- 3 methods, comment/uncomment each for speed comparisons
Line 630 ⟶ 2,075:
-- print $ inorder $ dutch1 s -- O(n)
print $ inorder $ dutch2 s -- O(n)
where s = mk012 10000000 42</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
The following solution works in both languages.
 
The problem statement isn't clear on whether the randomized list of balls
has to contain at least one of each color. The approach below assumes that
you can have no balls of a given color (including no balls at all - though
that makes ensuring they're not properly sorted at the start hard...). To
force at least one of each color ball, change "?n-1" to "?n" in the 3rd line.
 
<syntaxhighlight lang="unicon">procedure main(a)
n := integer(!a) | 20
every (nr|nw|nb) := ?n-1
sIn := repl("r",nw)||repl("w",nb)||repl("b",nr)
write(sRand := bestShuffle(sIn))
write(sOut := map(csort(map(sRand,"rwb","123")),"123","rwb"))
if sIn ~== sOut then write("Eh? Not in correct order!")
end
 
procedure bestShuffle(s) # (Taken from the Best Shuffle task)
t := s
every !t :=: ?t # Uncommented to get a random best shuffling
every i := 1 to *t do
every j := (1 to i-1) | (i+1 to *t) do
if (t[i] ~== s[j]) & (s[i] ~== t[j]) then break t[i] :=: t[j]
return t
end
 
procedure csort(w)
every (s := "") ||:= (find(c := !cset(w),w),c)
return s
end</syntaxhighlight>
 
A few sample runs:
 
<pre>
->dutch
bwwwwwwwwwrrrrrrbbbrrbrwwwrw
rrrrrrrrrrwwwwwwwwwwwwwbbbbb
->dutch
bbbbbbrbbbbbbrwwrwwrwwwwrw
rrrrrwwwwwwwwwbbbbbbbbbbbb
->dutch
bbbbbbbbbwbbwrrrrrrrrrwrrwwrr
rrrrrrrrrrrrrwwwwwbbbbbbbbbbb
->dutch
wbrbrrwwrbrbwrrrrrrwrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrwwwwwbbbb
->
</pre>
 
=={{header|J}}==
We shall define a routine to convert the values 0 1 2 to ball names:
<langsyntaxhighlight Jlang="j">i2b=: {&(;:'red white blue')</langsyntaxhighlight>
and its inverse
<langsyntaxhighlight Jlang="j">b2i=: i2b inv</langsyntaxhighlight>
Next, we need a random assortment of balls:
<langsyntaxhighlight Jlang="j"> BALLS=: i2b ?20#3
BALLS
┌────┬───┬────┬───┬───┬─────┬─────┬─────┬────┬────┬─────┬────┬────┬───┬────┬───┬─────┬───┬────┬───┐
│blue│red│blue│red│red│white│white│white│blue│blue│white│blue│blue│red│blue│red│white│red│blue│red│
└────┴───┴────┴───┴───┴─────┴─────┴─────┴────┴────┴─────┴────┴────┴───┴────┴───┴─────┴───┴────┴───┘</langsyntaxhighlight>
And we want to sort them in their canonical order:
<langsyntaxhighlight Jlang="j"> /:~&.b2i BALLS
┌───┬───┬───┬───┬───┬───┬───┬─────┬─────┬─────┬─────┬─────┬────┬────┬────┬────┬────┬────┬────┬────┐
│red│red│red│red│red│red│red│white│white│white│white│white│blue│blue│blue│blue│blue│blue│blue│blue│
└───┴───┴───┴───┴───┴───┴───┴─────┴─────┴─────┴─────┴─────┴────┴────┴────┴────┴────┴────┴────┴────┘</langsyntaxhighlight>
Note that if we were not using J's built in sort, we would probably want to use [[Counting_sort|bin sort]] here.
 
Anyways, we can test that they are indeed sorted properly:
<langsyntaxhighlight Jlang="j"> assert@(-: /:~)&b2i /:~&.b2i BALLS</langsyntaxhighlight>
 
=={{header|Java}}==
The elements of an <code>enum</code> implement <code>Comparable</code> so the build-in sort works. You can also use this comparability to check the sort has worked.
<syntaxhighlight lang="java">import java.util.Arrays;
import java.util.Random;
 
public class DutchNationalFlag {
enum DutchColors {
RED, WHITE, BLUE
}
 
public static void main(String[] args){
DutchColors[] balls = new DutchColors[12];
DutchColors[] values = DutchColors.values();
Random rand = new Random();
 
for (int i = 0; i < balls.length; i++)
balls[i]=values[rand.nextInt(values.length)];
System.out.println("Before: " + Arrays.toString(balls));
 
Arrays.sort(balls);
System.out.println("After: " + Arrays.toString(balls));
 
boolean sorted = true;
for (int i = 1; i < balls.length; i++ ){
if (balls[i-1].compareTo(balls[i]) > 0){
sorted=false;
break;
}
}
System.out.println("Correctly sorted: " + sorted);
}
}</syntaxhighlight>
 
{{out}}
<pre>Before: [WHITE, RED, BLUE, RED, WHITE, WHITE, WHITE, RED, WHITE, RED, WHITE, WHITE]
After: [RED, RED, RED, RED, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, BLUE]
Correctly sorted: true</pre>
 
=={{header|Javascript}}==
===ES6===
<syntaxhighlight lang="javascript">const dutchNationalFlag = () => {
 
/**
* Return the name of the given number in this way:
* 0 = Red
* 1 = White
* 2 = Blue
* @param {!number} e
*/
const name = e => e > 1 ? 'Blue' : e > 0 ? 'White' : 'Red';
 
/**
* Given an array of numbers return true if each number is bigger than
* or the same as the previous
* @param {!Array<!number>} arr
*/
const isSorted = arr => arr.every((e,i) => e >= arr[Math.max(i-1, 0)]);
 
/**
* Generator that keeps yielding a random int between 0(inclusive) and
* max(exclusive), up till n times, and then is done.
* @param max
* @param n
*/
function* randomGen (max, n) {
let i = 0;
while (i < n) {
i += 1;
yield Math.floor(Math.random() * max);
}
}
 
/**
* An array of random integers between 0 and 3
* @type {[!number]}
*/
const mixedBalls = [...(randomGen(3, 22))];
 
/**
* Sort the given array into 3 sub-arrays and then concatenate those.
*/
const sortedBalls = mixedBalls
.reduce((p,c) => p[c].push(c) && p, [[],[],[]])
.reduce((p,c) => p.concat(c), []);
 
/**
* A verbatim implementation of the Wikipedia pseudo-code
* @param {!Array<!number>} A
* @param {!number} mid The value of the 'mid' number. In our case 1 as
* low is 0 and high is 2
*/
const dutchSort = (A, mid) => {
let i = 0;
let j = 0;
let n = A.length - 1;
while(j <= n) {
if (A[j] < mid) {
[A[i], A[j]] = [A[j], A[i]];
i += 1;
j += 1;
} else if (A[j] > mid) {
[A[j], A[n]] = [A[n], A[j]];
n -= 1
} else {
j += 1;
}
}
};
 
console.log(`Mixed balls : ${mixedBalls.map(name).join()}`);
console.log(`Is sorted: ${isSorted(mixedBalls)}`);
 
console.log(`Sorted balls : ${sortedBalls.map(name).join()}`);
console.log(`Is sorted: ${isSorted(sortedBalls)}`);
 
// Only do the dutch sort now as it mutates the mixedBalls array in place.
dutchSort(mixedBalls, 1);
console.log(`Dutch Sorted balls: ${mixedBalls.map(name).join()}`);
console.log(`Is sorted: ${isSorted(mixedBalls)}`);
};
dutchNationalFlag();
</syntaxhighlight>
{{out}}
<pre>
Mixed balls : Red,Red,Blue,Red,White,Red,White,Blue,Blue,White,White,Blue,Red,Blue,Blue,Red,White,Red,Red,Red,White,White
Is sorted: false
Sorted balls : Red,Red,Red,Red,Red,Red,Red,Red,Red,White,White,White,White,White,White,White,Blue,Blue,Blue,Blue,Blue,Blue
Is sorted: true
Dutch Sorted balls: Red,Red,Red,Red,Red,Red,Red,Red,Red,White,White,White,White,White,White,White,Blue,Blue,Blue,Blue,Blue,Blue
Is sorted: true
</pre>
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
 
'''Adapted from [[#Wren|Wren]]'''
 
In the following, /dev/random is used as a source of entropy.
In a bash or bash-like environment, a suitable invocation would
be as follows:
<pre>
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr -f dnf.jq
</pre>
'''dnf.jq'''
<syntaxhighlight lang=jq>
# Output: a PRN in range(0; .)
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
def colors: ["Red", "White", "Blue"];
 
def colorMap: {"Red": 0, "White": 1, "Blue": 2 };
 
def task($nballs):
def sorted:
. == sort_by(colorMap[.]);
def generate:
[range(0; $nballs) | colors[ 3|prn ] ]
| if sorted then generate else . end;
generate
| "Before sorting : \(.)",
"After sorting : \(sort_by(colorMap[.]))" ;
 
task(9)
</syntaxhighlight>
{{output}}
<pre>
Before sorting : ["Blue","Red","Blue","White","Blue","White","Red","White","Blue"]
After sorting : ["Red","Red","White","White","White","Blue","Blue","Blue","Blue"]
</pre>
 
=={{header|Julia}}==
Here the task is solved two ways, with a specialized routine and using the <tt>sort</tt> built-in. <tt>dutchsort</tt> is a specialized sort based upon the <tt>three-way-partition</tt> pseudocode provided in the Wikipedia article referenced in the task description. Timing each shows that <tt>dutchsort</tt> is about two orders of magnitude faster than <tt>sort</tt>. (This relative performance result holds for a variety of color array sizes.)
 
'''Function'''
<syntaxhighlight lang="julia">
const COLORS = ["red", "white", "blue"]
 
function dutchsort!(a::Array{ASCIIString,1}, lo=COLORS[1], hi=COLORS[end])
i = 1
j = 1
n = length(a)
while j <= n
if a[j] == lo
a[i], a[j] = a[j], a[i]
i += 1
j += 1
elseif a[j] == hi
a[j], a[n] = a[n], a[j]
n -= 1
else
j += 1
end
end
return a
end
 
function dutchsort(a::Array{ASCIIString,1}, lo=COLORS[1], hi=COLORS[end])
dutchsort!(copy(a), lo, hi)
end
</syntaxhighlight>
 
'''Main'''
<syntaxhighlight lang="julia">
function formatdf(a::Array{ASCIIString,1})
i = 0
s = " "
for c in a
s *= @sprintf "%6s" c
i += 1
i %= 8
if i == 0
s *= "\n "
end
end
return s
end
 
cnum = 20
d = [COLORS[rand(1:3)] for i in 1:cnum]
while d == dutchsort(d)
d = [COLORS[rand(1:3)] for i in 1:cnum]
end
 
println("The original list is:")
println(formatdf(d))
 
print("Sorting with dutchsort, ")
@time e = dutchsort(d)
println(formatdf(e))
 
print("Sorting conventionally, ")
@time e = sort(d, by=x->findfirst(COLORS, x))
println(formatdf(e))
</syntaxhighlight>
 
{{out}}
<pre>
The original list is:
red blue red blue white blue white white
blue white white blue white white blue white
white blue blue blue
Sorting with dutchsort, elapsed time: 0.000520454 seconds (14104 bytes allocated)
red red white white white white white white
white white white blue blue blue blue blue
blue blue blue blue
Sorting conventionally, elapsed time: 0.062974782 seconds (1688896 bytes allocated)
red red white white white white white white
white white white blue blue blue blue blue
blue blue blue blue
</pre>
 
=={{header|Kotlin}}==
{{trans|D}}
<syntaxhighlight lang="scala">// 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()}")
}</syntaxhighlight>
 
Sample output:
<pre>
Before sorting : [WHITE, RED, RED, WHITE, BLUE, WHITE, BLUE, RED, RED]
After sorting : [RED, RED, RED, RED, WHITE, WHITE, WHITE, BLUE, BLUE]
</pre>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">define orderdutchflag(a) => {
local(r = array, w = array, b = array)
with i in #a do => {
match(#i) => {
case('Red')
#r->insert(#i)
case('White')
#w->insert(#i)
case('Blue')
#b->insert(#i)
}
}
return #r + #w + #b
}
 
orderdutchflag(array('Red', 'Red', 'Blue', 'Blue', 'Blue', 'Red', 'Red', 'Red', 'White', 'Blue'))</syntaxhighlight>
{{out}}
<pre>array(Red, Red, Red, Red, Red, White, Blue, Blue, Blue, Blue)</pre>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">; We'll just use words for the balls
make "colors {red white blue}
 
Line 721 ⟶ 2,556:
setitem :a :array item :b :array
setitem :b :array :temp
end</langsyntaxhighlight>
 
Test code:
<syntaxhighlight lang="text">do.while [
make "list random_balls 10
] [dutch? :list]
Line 730 ⟶ 2,565:
print (sentence [Start list:] arraytolist :list)
print (sentence [Sorted:] arraytolist dutch :list)
bye</langsyntaxhighlight>
 
{{out}}
Output:
<pre>Start list: white blue red red red white blue red red white
Sorted: red red red red red white white white blue blue</pre>
 
=={{header|MathematicaLua}}==
The task seems to allow for some interpretation, so attempting to follow as literally as possible.
<lang Mathematica>flagSort[data_List] := Sort[data, (#1 === RED || #2 === BLUE) &]</lang>
<syntaxhighlight lang="lua">-- "1. Generate a randomized order of balls.."
math.randomseed(os.time())
N, balls, colors = 10, {}, { "red", "white", "blue" }
for i = 1, N do balls[i] = colors[math.random(#colors)] end
-- "..ensuring that they are not in the order of the Dutch national flag."
order = { red=1, white=2, blue=3 }
function issorted(t)
for i = 2, #t do
if order[t[i]] < order[t[i-1]] then return false end
end
return true
end
local function shuffle(t)
for i = #t, 2, -1 do
local j = math.random(i)
t[i], t[j] = t[j], t[i]
end
end
while issorted(balls) do shuffle(balls) end
print("RANDOM: "..table.concat(balls,","))
 
-- "2. Sort the balls in a way idiomatic to your language."
table.sort(balls, function(a, b) return order[a] < order[b] end)
 
-- "3. Check the sorted balls are in the order of the Dutch national flag."
print("SORTED: "..table.concat(balls,","))
print(issorted(balls) and "Properly sorted." or "IMPROPERLY SORTED!!")</syntaxhighlight>
{{out}}
<pre>RANDOM: white,white,blue,blue,red,red,blue,white,red,white
SORTED: red,red,red,white,white,white,white,blue,blue,blue
Properly sorted.</pre>
 
=={{header|M2000 Interpreter}}==
Most times the Three Way Partition makes more changed than the first algorithm. Also if the array is sorted from the start, no changes give the first algorithm and 23 changes the Three Way Partition,
 
<syntaxhighlight lang="m2000 interpreter">
Report "Dutch Flag from Dijkstra"
const center=2
enum balls {Red, White, Blue}
fillarray=lambda a=(Red, White, Blue) (size as long=10)-> {
if size<1 then size=1
randomitem=lambda a->a#val(random(0,2))
dim a(size)<<randomitem()
=a()
}
Display$=lambda$ (s as array) ->{
Document r$=eval$(array(s))
if len(s)>1 then
For i=1 to len(s)-1 {
r$=", "+eval$(array(s,i))
}
end if
=r$
}
TestSort$=lambda$ (s as array)-> {
="unsorted: "
x=array(s)
for i=1 to len(s)-1 {
k=array(s,i)
if x>k then break
swap x, k
}
="sorted: "
}
Positions=lambda mid=White (a as array) ->{
m=len(a)
dim Base 0, b(m)=-1
low=-1
high=m
m--
i=0
medpos=stack
link a to a()
for i=m to 0 {
if a(i)<=mid then exit
high--
b(high)=high
}
for i=0 to m {
if a(i)>=mid then exit
low++
b(low)=low
}
if high-low>1 then
for i=low+1 to high-1 {
select case a(i)<=>Mid
case -1
low++ : b(low)=i
case 1
{
high-- :b(high)=i
if High<i then swap b(high), b(i)
}
else case
stack medpos {data i}
end select
}
end if
if Len(medpos)>0 then
dim c()
c()=array(medpos)
stock c(0) keep len(c()), b(low+1)
for i=low+1 to high-1
if b(i)>low and b(i)<high and b(i)<>i then swap b(b(i)), b(i)
next i
end if
if low>0 then
for i=0 to low
if b(i)<=low and b(i)<>i then swap b(b(i)), b(i)
next
end if
if High<m then
for i=m to High
if b(i)>=High and b(i)<>i then swap b(b(i)), b(i)
next
end if
=b()
}
InPlace=Lambda (&p(), &Final()) ->{
def i=0, j=-1, k=-1, many=0
for i=0 to len(p())-1
if p(i)<>i then
j=i
z=final(j)
do
final(j)=final(p(j))
k=j
j=p(j)
p(k)=k
many++
until j=i
final(k)=z
end if
next
=many
}
 
 
Dim final(), p(), second(), p1()
Rem final()=(White,Red,Blue,White,Red, Red, Blue)
Rem final()=(white, blue, red, blue, white)
 
final()=fillarray(30)
Print "Items: ";len(final())
Report TestSort$(final())+Display$(final())
\\ backup for final() for second example
second()=final()
p()=positions(final())
\\ backup p() to p1() for second example
p1()=p()
 
 
Report Center, "InPlace"
rem Print p() ' show array items
many=InPlace(&p(), &final())
rem print p() ' show array items
Report TestSort$(final())+Display$(final())
print "changes: "; many
 
 
Report Center, "Using another array to make the changes"
final()=second()
\\ using a second array to place only the changes
item=each(p1())
many=0
While item {
if item^=array(item) else final(item^)=second(array(item)) : many++
}
Report TestSort$(final())+Display$(final())
print "changes: "; many
Module three_way_partition (A as array, mid as balls, &swaps) {
Def i, j, k
k=Len(A)
Link A to A()
While j < k
if A(j) < mid Then
Swap A(i), A(j)
swaps++
i++
j++
Else.if A(j) > mid Then
k--
Swap A(j), A(k)
swaps++
Else
j++
End if
End While
}
Many=0
Z=second()
Print
Report center, {Three Way Partition
}
Report TestSort$(Z)+Display$(Z)
three_way_partition Z, White, &many
Print
Report TestSort$(Z)+Display$(Z)
Print "changes: "; many
 
</syntaxhighlight>
 
{{out}}
<pre>
Dutch Flag from Dijkstra
Items: 30
unsorted: Red, White, White, Red, Red, White, Blue, Red, Red, Blue, Red, Blue, White, White, Red, White, Blue, Blue, White, Blue, Red, Blue, Blue, White, Blue, White, Blue, Red, White, White
InPlace
sorted: Red, Red, Red, Red, Red, Red, Red, Red, Red, White, White, White, White, White, White, White, White, White, White, White, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue
changes: 20
Using another array to make the changes
sorted: Red, Red, Red, Red, Red, Red, Red, Red, Red, White, White, White, White, White, White, White, White, White, White, White, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue
changes: 20
Three Way Partition
unsorted: Red, White, White, Red, Red, White, Blue, Red, Red, Blue, Red, Blue, White, White, Red, White, Blue, Blue, White, Blue, Red, Blue, Blue, White, Blue, White, Blue, Red, White, White
sorted: Red, Red, Red, Red, Red, Red, Red, Red, Red, White, White, White, White, White, White, White, White, White, White, White, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue, Blue
changes: 19
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">flagSort[data_List] := Sort[data, (#1 === RED || #2 === BLUE) &]</syntaxhighlight>
{{out}}
<pre>flagSort[{WHITE, RED, RED, WHITE, WHITE, BLUE, WHITE, BLUE, BLUE, WHITE, WHITE, BLUE}]
 
{RED, RED, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, BLUE, BLUE, BLUE, BLUE}</pre>
 
=={{header|Nim}}==
We have chosen to use the sorting algorithm proposed by Dijkstra. To switch from our solution to one using Nim sorting algorithm, one has to add “import algorithm” at the beginning and to replace the lines <code>var sortedColors = colors</code> and <code>threeWayPartition(sortedColors, White)</code> by the single line <code>let sortedColors = sorted(colors)</code>.
 
The number of colors may be specified as argument in the command line. By default, 10 colors are randomly chosen.
 
<syntaxhighlight lang="nim">import os, random, strutils
 
type Color {.pure.} = enum Red = "R", White = "W", Blue = "B"
 
#---------------------------------------------------------------------------------------------------
 
proc isSorted(a: openArray[Color]): bool =
# Check if an array of colors is in the order of the dutch national flag.
var prevColor = Red
for color in a:
if color < prevColor:
return false
prevColor = color
return true
 
#---------------------------------------------------------------------------------------------------
 
proc threeWayPartition(a: var openArray[Color]; mid: Color) =
## Dijkstra way to sort the colors.
var i, j = 0
var k = a.high
while j <= k:
if a[j] < mid:
swap a[i], a[j]
inc i
inc j
elif a[j] > mid:
swap a[j], a[k]
dec k
else:
inc j
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
var n: Positive = 10
 
# Get the number of colors.
if paramCount() > 0:
try:
n = paramStr(1).parseInt()
if n <= 1:
raise newException(ValueError, "")
except ValueError:
echo "Wrong number of colors"
quit(QuitFailure)
 
# Create the colors.
randomize()
var colors = newSeqOfCap[Color](n)
 
while true:
for i in 0..<n:
colors.add(Color(rand(ord(Color.high))))
if not colors.isSorted():
break
colors.setLen(0) # Reset for next try.
 
echo "Original: ", colors.join("")
 
# Sort the colors.
var sortedColors = colors
threeWayPartition(sortedColors, White)
doAssert sortedColors.isSorted()
echo "Sorted: ", sortedColors.join("")</syntaxhighlight>
 
{{out}}
With 10 colors:
<pre>Original: WWWRBRWWBW
Sorted: RRWWWWWWBB</pre>
 
=={{header|PARI/GP}}==
A [[counting sort]] might be more appropriate here, but that would conceal the details of the sort.
<langsyntaxhighlight lang="parigp">compare(a,b)={
if (a==b,
0
Line 758 ⟶ 2,889:
while(inorder(v), v=r(10));
v=vecsort(v,compare);
inorder(v)</langsyntaxhighlight>
 
{{out}}
Output:
<pre>1</pre>
 
=={{header|Perl 6}}==
The task is probably not to just sort an array. The wikipedia links has a slightly better explanation that leads to the following code:
Here are five ways to do it, all one liners (apart from the test apparatus).
<syntaxhighlight lang="perl">use warnings;
<lang>enum NL <red white blue>;
use strict;
my @colors;
use 5.010; # //
 
use List::Util qw( shuffle );
sub how'bout (&this-way) {
 
sub show {
my @colours = qw( blue white red );
say @colors;
 
say "Ordered: ", [<=] @colors;
sub are_ordered {
my $balls = shift;
my $last = 0;
for my $ball (@$balls) {
return if $ball < $last;
$last = $ball;
}
return 1;
}
 
 
@colors = NL.roll(20);
sub show {
show;
this-waymy $balls = shift;
print join(' ', map $colours[$_], @$balls), "\n";
show;
say '';
}
 
say "Using functional sort";
how'bout { @colors = sort *.value, @colors }
 
sub debug {
say "Using in-place sort";
return unless $ENV{DEBUG};
how'bout { @colors .= sort: *.value }
 
my ($pos, $top, $bottom, $balls) = @_;
say "Using a Bag";
for my $i (0 .. $#$balls) {
how'bout { @colors = red, white, blue Zxx bag(@colors».key)<red white blue> }
my ($prefix, $suffix) = (q()) x 2;
 
($prefix, $suffix) = qw/( )/ if $i == $pos;
say "Using the classify method";
$prefix .= '>' if $i == $top;
how'bout { @colors = (.list for %(@colors.classify: *.value){0,1,2}) }
$suffix .= '<' if $i == $bottom;
 
print STDERR " $prefix$colours[$balls->[$i]]$suffix";
say "Using multiple greps";
}
how'bout { @colors = (.grep(red), .grep(white), .grep(blue) given @colors) }</lang>
print STDERR "\n";
}
 
 
my $count = shift // 10;
die "$count: Not enough balls\n" if $count < 3;
 
my $balls = [qw( 2 1 0 )];
push @$balls, int rand 3 until @$balls == $count;
do { @$balls = shuffle @$balls } while are_ordered($balls);
 
show($balls);
 
my $top = 0;
my $bottom = $#$balls;
 
my $i = 0;
while ($i <= $bottom) {
debug($i, $top, $bottom, $balls);
my $col = $colours[ $balls->[$i] ];
if ('red' eq $col and $i < $bottom) {
@{$balls}[$bottom, $i] = @{$balls}[$i, $bottom];
$bottom--;
} elsif ('blue' eq $col and $i > $top) {
@{$balls}[$top, $i] = @{$balls}[$i, $top];
$top++;
} else {
$i++;
}
}
debug($i, $top, $bottom, $balls);
 
show($balls);
are_ordered($balls) or die "Incorrect\n";</syntaxhighlight>
You can run it with no parameters, it sorts 10 balls in such a case. If you provide one parameter, it is used as the number of balls. The second parameter turns on debugging that shows how the balls are being swapped.
 
=={{header|Phix}}==
Minimizes the number of read and swap operations, straight translation of the wikipedia pseudocode:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">three_way_partition</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">mid</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">mid</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]}</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">j</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">mid</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]}</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">j</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">colours</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"red"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"white"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"blue"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">enum</span> <span style="color: #000080;font-style:italic;">/*red,*/</span> <span style="color: #000000;">white</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">blue</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">blue</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">msg</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">msg</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">unsorted</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sorted</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">unsorted</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_rand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">maxc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">))</span>
<span style="color: #000080;font-style:italic;">-- sorted = sort(deep_copy(unsorted)) -- (works just as well)</span>
<span style="color: #000000;">sorted</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">three_way_partition</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unsorted</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">white</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">unsorted</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">sorted</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"oops"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Unsorted"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">unsorted</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Sorted"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sorted</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
<small>I thought of unsorted=shuffle(unsorted) in the "oops" loop, but of course that'd repeat forever should they all be the same colour.</small>
{{out}}
<pre>
<pre>Using functional sort
redUnsorted: redblue whiteblue whiteblue redblue red red red red red redwhite white red white red red red white white whiteblue
Sorted: red red red white white white white blue blue blue blue blue
Ordered: False
</pre>
red red red red red red red red red red red red red white white white white white white white
Ordered: True
 
=={{header|Picat}}==
Using in-place sort
<syntaxhighlight lang="picat">go =>
red blue white red white blue white blue red white blue blue blue red white white red blue red blue
_ = random2(), % random seed
Ordered: False
N = 21,
red red red red red red white white white white white white blue blue blue blue blue blue blue blue
Map = new_map([1=red,2=white,3=blue]),
Ordered: True
[Rand,Sorted] = dutch_random_sort(N,Map,Map.inverse()),
println('rand '=Rand),
println(sorted=Sorted),
nl.
 
% generate a random order and ensure it's not already dutch sorted
Using a Bag
dutch_random_sort(N,Map,MapInv) = [Rand,Sorted] =>
red blue blue blue white red white red white blue blue red red red red blue blue red white blue
Rand = dutch_random1(N,Map),
Ordered: False
Sorted = dutch_sort(Rand,MapInv),
red red red red red red red red white white white white blue blue blue blue blue blue blue blue
while (Rand == Sorted)
Ordered: True
println("Randomize again"),
Rand := dutch_random1(N,Map),
Sorted := dutch_sort(Rand,MapInv)
end.
 
dutch_random1(N,Map) = [Map.get(1+(random() mod Map.map_to_list().length)) : _I in 1..N].
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
 
dutch_sort(L,MapInv) = [R : _=R in [MapInv.get(R)=R : R in L].sort()].
Using multiple greps
 
red white blue white white red blue white red white red white white white white white red red blue red
inverse(Map) = new_map([V=K : K=V in Map]).</syntaxhighlight>
Ordered: False
 
red red red red red red red white white white white white white white white white white blue blue blue
{{out}}
Ordered: True</pre>
<pre>rand = [red,blue,white,white,white,blue,blue,blue,red,red,blue,white,blue,blue,red,white,blue,blue,white,white,red]
sorted = [red,red,red,red,red,white,white,white,white,white,white,white,blue,blue,blue,blue,blue,blue,blue,blue,blue]</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(def 'Colors
(list
(def 'RED 1)
Line 839 ⟶ 3,063:
(prin "Sorted balls ")
(print S)
(prinl " are sorted") )</langsyntaxhighlight>
{{out}}
Output:
<pre>Original balls (RED BLUE WHITE BLUE BLUE RED WHITE WHITE WHITE) not sorted
Sorted balls (RED RED WHITE WHITE WHITE WHITE BLUE BLUE BLUE) are sorted</pre>
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
$Colors = 'red', 'white','blue'
# Select 10 random colors
$RandomBalls = 1..10 | ForEach { $Colors | Get-Random }
# Ensure we aren't finished before we start. For some reason. It's in the task requirements.
While ( $RandomBalls -eq $RandomBalls | Sort { $Colors.IndexOf( $_ ) } )
{ $RandomBalls = 1..10 | ForEach { $Colors | Get-Random } }
# Sort the colors
$SortedBalls = $RandomBalls | Sort { $Colors.IndexOf( $_ ) }
# Display the results
$RandomBalls
''
$SortedBalls
</syntaxhighlight>
{{out}}
<pre>
white
blue
blue
blue
white
red
white
blue
red
red
 
red
red
red
white
white
white
blue
blue
blue
blue
</pre>
 
=={{header|Prolog}}==
Works with SWI-Prolog 6.1.11
===Prolog spirit===
<langsyntaxhighlight Prologlang="prolog">dutch_flag(N) :-
length(L, N),
repeat,
Line 906 ⟶ 3,175:
 
is_dutch_flag_blue([]).
</syntaxhighlight>
</lang>
{{out}}
Output :
<pre> ?- dutch_flag(20).
[blue,white,white,blue,blue,blue,red,blue,red,blue,blue,blue,white,red,red,blue,blue,red,blue,red]
Line 919 ⟶ 3,188:
===Functional spirit===
Use of filters.
<langsyntaxhighlight Prologlang="prolog">dutch_flag(N) :-
length(L, N),
 
Line 981 ⟶ 3,250:
 
is_dutch_flag_blue([]).
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
===Python: Sorted===
The heart of the idiomatic Dutch sort in python is the call to function <code>sorted</code> in function <code>dutch_flag_sort</code>.
<langsyntaxhighlight lang="python">import random
 
colours_in_order = 'Red White Blue'.split()
Line 1,020 ⟶ 3,289:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{out|Sample output}}
<pre>Original Ball order: ['Red', 'Red', 'Blue', 'Blue', 'Blue', 'Red', 'Red', 'Red', 'White', 'Blue']
Line 1,026 ⟶ 3,295:
 
===Python: sum of filters===
This follows the [[wp:Dutch_national_flag_problem#Critic|critics section]] of the wikipedia article by using a sum of filters.
of the wikipedia article by using a sum of filters.
 
Replace the function/function call dutch_flag_sort above, with dutch_flag_sort2 defined as:
<langsyntaxhighlight lang="python">from itertools import chain
def dutch_flag_sort2(items, order=colours_in_order):
'return summed filter of items using the given order'
return list(chain.from_iterable(filter(lambda c: c==colour, items)
for colour in order))</langsyntaxhighlight>
 
Or equivalently using a list comprehension (though perhaps less clear):
<langsyntaxhighlight lang="python">def dutch_flag_sort2(items, order=colours_in_order):
'return summed filter of items using the given order'
return [c for colour in order for c in items if c==colour]</langsyntaxhighlight>
Output follows that of the sorting solution above.
 
===Python: Construct from ball counts===
This reconstructs the correct output by counting how many of each colour theirthere are.
 
Replace the function/function call dutch_flag_sort above, with dutch_flag_sort3 defined as:
<langsyntaxhighlight lang="python">def dutch_flag_sort3(items, order=colours_in_order):
'counts each colour to construct flag'
return sum([[colour] * items.count(colour) for colour in order], [])</langsyntaxhighlight>
Output follows that of the sorting solution above.
 
===Python: Explicit in-place sort===
<langsyntaxhighlight lang="python">import random
 
colours_in_order = 'Red White Blue'.split()
Line 1,100 ⟶ 3,370:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
Output follows that of the sorting solution above.
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
#lang racket
 
(define dutch-colors '(red white blue))
 
(define (dutch-order? balls)
;; drop each color from the front, should end up empty
(null? (for/fold ([r balls]) ([color dutch-colors])
(dropf r (curry eq? color)))))
 
(define (random-balls)
(define balls
(for/list ([i (random 20)])
(list-ref dutch-colors (random (length dutch-colors)))))
(if (dutch-order? balls) (random-balls) balls))
 
;; first method: use a key to map colors to integers
(define (order->key order)
(let ([alist (for/list ([x order] [i (in-naturals)]) (cons x i))])
(λ(b) (cdr (assq b alist)))))
(define (sort-balls/key balls)
(sort balls < #:key (order->key dutch-colors)))
 
;; second method: use a comparator built from the ordered list
(define ((order<? ord) x y)
(memq y (cdr (memq x ord))))
(define (sort-balls/compare balls)
(sort balls (order<? dutch-colors)))
 
(define (test sort)
(define balls (random-balls))
(define sorted (sort balls))
(printf "Testing ~a:\n Random: ~s\n Sorted: ~s\n ==> ~s\n"
(object-name sort)
balls sorted (if (dutch-order? sorted) 'OK 'BAD)))
(for-each test (list sort-balls/key sort-balls/compare))
</syntaxhighlight>
 
{{out}}
<pre>
Testing sort-balls/order:
Random: (red blue blue white red blue red red blue blue red red white blue)
Sorted: (red red red red red red white white blue blue blue blue blue blue)
==> OK
Testing sort-balls/compare:
Random: (red blue white blue white white white blue red blue blue blue white)
Sorted: (red red white white white white white blue blue blue blue blue blue)
==> OK
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Here are five ways to do it, all one liners (apart from the test apparatus).
<syntaxhighlight lang="raku" line>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) }</syntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|REXX}}==
===colors (as words)===
This version uses a version of a bin sort with counts, and has been generalized to allow any number of colors.
<br><br>The REXX solution could've been simplified somewhat by the use of the '''countstr''' bif (but older REXX interpreters don't have).
 
The REXX solution could've been simplified somewhat by the use of the &nbsp; '''countstr''' &nbsp; BIF &nbsp; (but some older REXX interpreters don't have).
<lang rexx>/*REXX pgm to reorder a set of random colored balls into a correct order*/
/*which is the order of colors on the Dutch flag: red, white, blue. */
 
<syntaxhighlight lang="rexx">/*REXX program reorders a set of random colored balls into a correct order, which is the*/
parse arg N colors /*get user args from command line*/
/*────────────────────────────────── order of colors on the Dutch flag: red white blue.*/
if N=',' | N='' then N=15 /*use default number of balls. */
ifparse arg N colors N='' then N=15 /*use default number of balls. /*obtain optional arguments from the CL*/
if colorsN='' | N="," then colorsN=space('red15 white blue') /*Not specified? Then use defaultthe colorsdefault.*/
Ncolorsif colors=words('' then colors)= 'red white blue' /* " " /*count the number of colors. " " " " */
#=words(colors) /*count the number of colors specified.*/
@=word(colors,Ncolors) word(colors,1) /*ensure balls aren't in order. */
@=word(colors, #) word(colors, 1) /*ensure balls aren't already in order.*/
 
do g=3 to N do g=3 to N /*generate a random # of colored balls. */
@=@ word( colors, random(1, #) ) @=@ word(colors, /*append a random(1,Ncolors)) color to the @ list.*/
end /*g*/
 
say 'number of colored balls generated = ' N N ; say
say center(' original ball order: ', length(@), "─")
say @ ; say
$=; do j=1 for Ncolors#; ; _=word(colors,j)
_=word(colors, j); $=$ copies(_' ', countWords(_, @))
end /*j*/
say
say ' sorted ball order:'
say center(' sorted ball order ', length(@), "─")
say space($); say
say space($)
 
say
do k=2 to N /*ensure the balls are in order. */
do k=2 to N /*verify the balls are in correct order*/
if wordpos(word($,k),colors)>=wordpos(word($,k-1),colors) then iterate
if wordpos(word($,k), colors) >= wordpos(word($,k-1), colors) then iterate
say "The list of sorted balls isn't in proper order!"; exit 13
say "The list of sorted balls isn't in proper order!"; exit 13
end /*k*/
say
 
say 'The sorted colored ball list has been confirmed as being sorted correctly.'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────COUNTWORDS subroutine───────────────*/
countWords: procedure; parse arg ?,hay; s=1
do r=0 until _==0; _=wordpos(?, hay, s); s=_+1; end; /*r*/; return r</langsyntaxhighlight>
'''output''' &nbsp; when using the default input:
<pre>
<pre style="overflow:scroll">
number of colored balls generated = 15
 
────────────────────────── original ball order ───────────────────────────
original ball order:
blue red white whiteblue white white red blue whiteblue blue red blue red blue white red
 
sorted ball order:
red red red red red white white white white white white blue blue blue blue
 
─────────────────────────── sorted ball order ───────────────────────────
sorted ball list has been confirmed as being sorted correctly.
red red red red white white white white blue blue blue blue blue blue blue
</pre>
 
===colors as letters===
<lang rexx>/*REXX pgm to reorder a set of random colored balls into a correct order*/
/*which is the order of colors on the Dutch flag: red, white, blue. */
 
The sorted colored ball list has been confirmed as being sorted correctly.
parse arg N colors . /*get user args from command line*/
</pre>
if N==',' | N=='' then N=15 /*use default number of balls. */
if colors='' then colors='RWB' /*default: R=red, W=white, B=blue*/
Ncolors=length(colors) /*count the number of colors. */
@=right(colors,1)left(colors,1) /*ensure balls aren't in order. */
 
===colors (as letters)===
do g=3 to N /*generate a random # of balls. */
<syntaxhighlight lang="rexx">/*REXX program reorders a set of random colored balls into a correct order, which is the*/
@=@ || substr(colors,random(1,Ncolors),1)
/*────────────────────────────────── order of colors on the Dutch flag: red white blue.*/
end /*g*/
parse arg N colors /*obtain optional arguments from the CL*/
if N='' | N="," then N=15 /*Not specified? Then use the default.*/
if colors='' then colors= "RWB" /*use default: R=red, W=white, B=blue */
#=length(colors) /*count the number of colors specified.*/
@=right(colors, 1)left(colors, 1) /*ensure balls aren't already in order.*/
 
do g=3 to N /*generate a random # of colored balls.*/
say 'number of colored balls generated = ' N ; say
@=@ ||substr( colors, random(1, #), 1) /*append a color (1char) to the @ list.*/
say 'original ball order:'
end /*g*/
say @ ; say
$=; do j=1 for Ncolors; _=substr(colors,j,1)
#=length(@)-length(space(translate(@,,_),0))
$=$||copies(_,#)
end /*j*/
say ' sorted ball order:'
say $; say
 
say 'number of colored balls generated = ' N ; say
do k=2 to N /*ensure the balls are in order. */
say center(' original ball order ', max(30,2*#), "─")
if pos(substr($,k,1),colors)>=pos(substr($,k-1,1),colors) then iterate
say @ ; say
say "The list of sorted balls isn't in proper order!"; exit 13
$=; do j=1 for #; _=substr(colors, j, 1)
#=length(@) - length( space( translate(@, , _), 0) )
$=$ || copies(_, #)
end /*j*/
say center(' sorted ball order ', max(30, 2*#), "─")
say $
say
do k=2 to N /*verify the balls are in correct order*/
if pos(substr($,k,1), colors) >= pos(substr($,k-1,1), colors) then iterate
say "The list of sorted balls isn't in proper order!"; exit 13
end /*k*/
say
 
say 'The sorted colored ball list has been confirmed as being sorted correctly.'
exit /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''output''' &nbsp; when using the default input:
<pre>
<pre style="overflow:scroll">
number of colored balls generated = 15
 
──── original ball order: ─────
BRRRRBWWRBWRRBR
BRBRRRWRBWRBBBR
 
───── sorted ball order: ─────
RRRRRRRRWWWBBBB
RRRRRRRWWBBBBBB
 
 
sorted ball list has been confirmed as being sorted correctly.
The sorted colored ball list has been confirmed as being sorted correctly.
 
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Dutch national flag problem
 
flag = ["Red","White","Blue"]
balls = list(10)
see "Random: |"
for i = 1 to 10
color = random(2) + 1
balls[i] = flag[color]
see balls[i] + " |"
next
see nl
see "Sorted: |"
for i = 1 to 3
color = flag[i]
for j = 1 to 10
if balls[j] = color
see balls[j] + " |"
ok
next
next
</syntaxhighlight>
Output:
<pre>
Random: |Red |Blue |Red |White |Red |Blue |White |Blue |Red |White |
Sorted: |Red |Red |Red |Red |White |White |White |Blue |Blue |Blue |
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">class Ball
<lang ruby>module Dutch
FLAG = {red: 1, white: 2, blue: 3}
# Could use a class for the balls, but that's a little heavy.
# We'll just use symbols.
def initialize
@color = FLAG.keys.sample
end
 
def color
# List of colors, in order
@color
Symbols = [:red, :white, :blue]
end
 
def <=>(other) # needed for sort, results in -1 for <, 0 for == and 1 for >.
# Reverse map from symbol to numeric value
FLAG[self.color] <=> FLAG[other.color]
Values = Hash[Symbols.each_with_index.to_a]
end
 
def inspect
# Pick a color at random
@color
def self.random_ball
Symbols[rand 3]
end
end
 
balls = []
# But we will use a custom subclass of Array for the list of balls
balls = Array.new(8){Ball.new} while balls == balls.sort
class Balls < Array
 
puts "Random: #{balls}"
# Generate a given-sized list of random balls
puts "Sorted: #{balls.sort}"
def self.random(n)
</syntaxhighlight>
self.new(n.times.map { Dutch.random_ball })
{{out}}
end
<pre>Random: [blue, red, red, red, blue, blue, white, red]
Sorted: [red, red, red, red, white, blue, blue, blue]
</pre>
 
=={{header|Run BASIC}}==
# Test to see if the list is already in order
<syntaxhighlight lang="runbasic">flag$ = "Red,White,Blue"
def dutch?
return true if length < 2
Values[self[0]] < Values[self[1]] && slice(1..-1).dutch?
end
 
print "Random: |";
# Traditional in-place sort
for i = 1 defto dutch!10
color = rnd(0) * 3 + lo = -1
balls$(i) = word$(flag$,color,",")
hi = length
print balls$(i);" |";
i = 0
next i
while i < hi do
case self[i]
when :red
lo += 1
self[lo], self[i] = self[i], self[lo]
i += 1
when :white
i += 1
when :blue
hi -= 1
self[hi], self[i] = self[i], self[hi]
end
end
self
end
 
print :print "Sorted: |";
# Recursive, non-self-modifying version
for i = 1 to 3
def dutch(acc = { :red => 0, :white => 0, :blue => 0})
color$ = word$(flag$,i,",")
return self.class.new(
for j = 1 to 10
Symbols.map { |c| [c] * acc[c] }.inject(&:+)
if balls$(j) if= lengthcolor$ ==then 0
print balls$(j);" |";
acc[first]+=1
end if
return slice(1..-1).dutch( acc )
next j
end
next i</syntaxhighlight>
end
<pre>Random: |White |Blue |White |Red |Red |White |Red |Blue |Red |White |
end</lang>
Sorted: |Red |Red |Red |Red |White |White |White |White |Blue |Blue |</pre>
 
=={{header|Rust}}==
Driver/test code:
{{libheader|rand}}
<lang ruby>balls = nil
<syntaxhighlight lang="rust">extern crate rand;
while balls.nil? or balls.dutch? do
balls = Dutch::Balls.random 8
end
puts "Start: #{balls}"
puts "Sorted: #{balls.dutch}"
puts "Intact: #{balls}"
puts "In-place: #{balls.dutch!}"
puts "Modified: #{balls}"</lang>
 
use rand::Rng;
Output:
 
<pre>Start: [:red, :blue, :red, :white, :red, :red, :white, :blue]
// Color enums will be sorted by their top-to-bottom declaration order
Sorted: [:red, :red, :red, :red, :white, :white, :blue, :blue]
#[derive(Eq,Ord,PartialOrd,PartialEq,Debug)]
Intact: [:red, :blue, :red, :white, :red, :red, :white, :blue]
enum Color {
In-place: [:red, :red, :red, :red, :white, :white, :blue, :blue]
Red,
Modified: [:red, :red, :red, :red, :white, :white, :blue, :blue]</pre>
White,
Blue
}
 
fn is_sorted(list: &Vec<Color>) -> bool {
let mut state = &Color::Red;
for current in list.iter() {
if current < state { return false; }
if current > state { state = current; }
}
true
}
 
 
fn main() {
let mut rng = rand::thread_rng();
let mut colors: Vec<Color> = Vec::new();
 
for _ in 1..10 {
let r = rng.gen_range(0, 3);
if r == 0 { colors.push(Color::Red); }
else if r == 1 { colors.push(Color::White); }
else if r == 2 { colors.push(Color::Blue); }
}
 
while is_sorted(&colors) {
rng.shuffle(&mut colors);
}
 
println!("Before: {:?}", colors);
colors.sort();
println!("After: {:?}", colors);
if !is_sorted(&colors) {
println!("Oops, did not sort colors correctly!");
}
}</syntaxhighlight>
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">object FlagColor extends Enumeration {
type FlagColor = Value
val Red, White, Blue = Value
}
 
val genBalls = (1 to 10).map(i => FlagColor(scala.util.Random.nextInt(FlagColor.maxId)))
val sortedBalls = genBalls.sorted
val sorted = if (genBalls == sortedBalls) "sorted" else "not sorted"
 
println(s"Generated balls (${genBalls mkString " "}) are $sorted.")
println(s"Sorted balls (${sortedBalls mkString " "}) are sorted.")</syntaxhighlight>
 
{{out}}
<pre>Generated balls (Blue Blue Blue White Blue Blue Red Red Blue White) are not sorted.
Sorted balls (Red Red White White Blue Blue Blue Blue Blue Blue) are sorted.</pre>
 
=={{header|sed}}==
The first part of the task is skipped, as there is no possibility to create random data within ''sed'' itself.
<syntaxhighlight lang="sed">:la
s/\(WW*\)\([RB].*\)/\2\1/
t la
:lb
s/\(BB*\)\([RW].*\)/\2\1/
t lb
/^RR*WW*BB*$/!d</syntaxhighlight>
{{out}}
<pre>
$ echo WRRWRRRBBWBRRWBWWB | sed -f dutch_flag_sort.sed
RRRRRRRWWWWWWBBBBB
</pre>
 
=={{header|SQL}}==
<syntaxhighlight lang="sql">-- Create and populate tables
create table colours (id integer primary key, name varchar(5));
insert into colours (id, name) values ( 1, 'red' );
insert into colours (id, name) values ( 2, 'white');
insert into colours (id, name) values ( 3, 'blue' );
 
create table balls ( colour integer references colours );
insert into balls ( colour ) values ( 2 );
insert into balls ( colour ) values ( 2 );
insert into balls ( colour ) values ( 3 );
insert into balls ( colour ) values ( 2 );
insert into balls ( colour ) values ( 1 );
insert into balls ( colour ) values ( 3 );
insert into balls ( colour ) values ( 3 );
insert into balls ( colour ) values ( 2 );
 
-- Show the balls are unsorted
select
colours.name
from
balls
join colours on balls.colour = colours.id;
 
-- Show the balls in dutch flag order
select
colours.name
from
balls
join colours on balls.colour = colours.id
order by
colours.id;
 
-- Tidy up
drop table balls;
drop table colours;</syntaxhighlight>
{{out}}
<pre>COLOUR
------
white
white
blue
white
red
blue
blue
white
 
 
COLOUR
------
red
white
white
white
white
blue
blue
blue</pre>
# ''Generating a randomized order of balls ensuring that they are not in the order of the Dutch national flag.'' Hmm - just loaded some data - could do better here...
# ''Sort the balls in a way idiomatic to your language.'' Yup!
# ''Check the sorted balls are in the order of the Dutch national flag.'' Not checked beyond eyeballing - is there a db implementation that gets <tt>order by</tt> wrong??
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">// Algorithm from https://en.wikipedia.org/wiki/Dutch_national_flag_problem
func partition3<T: Comparable>(_ a: inout [T], mid: T) {
var i = 0
var j = 0
var k = a.count - 1
while j <= k {
if a[j] < mid {
a.swapAt(i, j);
i += 1;
j += 1;
} else if a[j] > mid {
a.swapAt(j, k);
k -= 1;
} else {
j += 1;
}
}
}
 
func isSorted<T: Comparable>(_ a: [T]) -> Bool {
var i = 0
let n = a.count
while i + 1 < n {
if a[i] > a[i + 1] {
return false
}
i += 1
}
return true
}
 
enum Ball : CustomStringConvertible, Comparable {
case red
case white
case blue
var description : String {
switch self {
case .red: return "red"
case .white: return "white"
case .blue: return "blue"
}
}
}
 
var balls: [Ball] = [ Ball.red, Ball.white, Ball.blue,
Ball.red, Ball.white, Ball.blue,
Ball.red, Ball.white, Ball.blue]
balls.shuffle()
print("\(balls)")
print("Sorted: \(isSorted(balls))")
 
partition3(&balls, mid: Ball.white)
print("\(balls)")
print("Sorted: \(isSorted(balls))")</syntaxhighlight>
 
{{out}}
<pre>
[white, blue, red, red, white, blue, red, blue, white]
Sorted: false
[red, red, red, white, white, white, blue, blue, blue]
Sorted: true
</pre>
 
=={{header|Tcl}}==
This isn't very efficient in terms of the sorting itself (and it happens to use <code>lsearch</code> twice in the comparator!) but it is very simple to write like this.
<langsyntaxhighlight lang="tcl"># The comparison function
proc dutchflagcompare {a b} {
set colors {red white blue}
Line 1,309 ⟶ 3,910:
} else {
puts "sort failed\n$sorted"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,315 ⟶ 3,916:
red red red red red red red white white white white white white white white white blue blue blue blue
</pre>
 
=={{header|uBasic/4tH}}==
This version is based on Edsger Dijkstra's original algorithm. The flag may come out a bit shredded , but it has been assembled the correct way.
<syntaxhighlight lang="text">s = 100
 
For x = 0 To s-1
@(x) = Rnd(3)
Next
' Edsger Dijkstra algorithm starts here
i = 0
j = 0
k = s-1
Do
While j < k+1
If @(j) = 0 Then ' case "red"
Push @(j) : @(j) = @(i) : @(i) = Pop()
i = i + 1 ' fairly efficient exchange
j = j + 1
Else If @(j) = 2 Then ' case "blue"
Push @(j) : @(j) = @(k) : @(k) = Pop()
k = k - 1 ' fairly efficient exchange
Else ' you'd expect case "white" here
j = j + 1
Endif Endif
Loop
' end of Dijkstra's algorithm
n = 0
 
For x = 0 To s-1 ' now show the whole shebang
If @(x) # n Then Print : n = @(x)
Print Chr(Peek ("RWB", @(x)));
Next
 
Print</syntaxhighlight>
{{Out}}
<pre>RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
 
0 OK, 0:858</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bash}}
<langsyntaxhighlight lang="bash">COLORS=(red white blue)
 
# to go from name to number, we make variables out of the color names (e.g. the
# (e.g. the variable "$red" has value "1").
for (( i=0; i<${#COLORS[@]}; ++i )); do
eval ${COLORS[i]}=$i
Line 1,375 ⟶ 4,017:
done
echo "${a[@]}"
}</langsyntaxhighlight>
 
Test code:
<langsyntaxhighlight lang="bash">declare -i len=${1:-10}
balls=()
while (( ${#balls[@]} < len )) || dutch? "${balls[@]}"; do
Line 1,385 ⟶ 4,027:
echo "Initial list: ${balls[@]}"
balls=($(dutch "${balls[@]}"))
echo "Sorted: ${balls[@]}"</langsyntaxhighlight>
 
{{out}}
Sample output:
<pre>Initial list: blue blue red blue red blue blue white blue red
Sorted: red red red white blue blue blue blue blue blue</pre>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
'Solution derived from http://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/.
 
'build an unsorted array with n elements
Function build_unsort(n)
flag = Array("red","white","blue")
Set random = CreateObject("System.Random")
Dim arr()
ReDim arr(n)
For i = 0 To n
arr(i) = flag(random.Next_2(0,3))
Next
build_unsort = arr
End Function
 
'sort routine
Function sort(arr)
lo = 0
mi = 0
hi = UBound(arr)
Do While mi <= hi
Select Case arr(mi)
Case "red"
tmp = arr(lo)
arr(lo) = arr(mi)
arr(mi) = tmp
lo = lo + 1
mi = mi + 1
Case "white"
mi = mi + 1
Case "blue"
tmp = arr(mi)
arr(mi) = arr(hi)
arr(hi) = tmp
hi = hi - 1
End Select
Loop
sort = Join(arr,",")
End Function
 
unsort = build_unsort(11)
WScript.StdOut.Write "Unsorted: " & Join(unsort,",")
WScript.StdOut.WriteLine
WScript.StdOut.Write "Sorted: " & sort(unsort)
WScript.StdOut.WriteLine
</syntaxhighlight>
 
{{Out}}
<pre>
Unsorted: blue,white,white,blue,red,red,blue,red,red,red,white,white
Sorted: red,red,red,red,red,white,white,white,white,blue,blue,blue
</pre>
 
=={{header|Visual FoxPro}}==
===SQL Version===
<syntaxhighlight lang="vfp">
CLOSE DATABASES ALL
LOCAL lcCollate As String, i As Integer, n As Integer
lcCollate = SET("Collate")
SET COLLATE TO "Machine"
*!* Colours table
CREATE CURSOR colours (id I UNIQUE, colour V(5))
INSERT INTO colours VALUES (1, "Red")
INSERT INTO colours VALUES (2, "White")
INSERT INTO colours VALUES (3, "Blue")
*!* Balls table
CREATE CURSOR balls (colour I, rowid I AUTOINC)
INDEX ON colour TAG colour
SET ORDER TO 0
*!* Make sure there is at least 1 of each colour
INSERT INTO balls (colour) VALUES(3)
INSERT INTO balls (colour) VALUES(1)
INSERT INTO balls (colour) VALUES(2)
RAND(-1) && Initialise random number generator
n = 24
FOR i = 4 TO n
INSERT INTO balls (colour) VALUES (RanInt())
ENDFOR
*!* Show unsorted
SELECT bb.rowid, cc.colour FROM colours cc JOIN balls bb ON cc.id = bb.colour
*!* Select by correct order
SELECT bb.rowid, cc.colour FROM colours cc JOIN balls bb ON cc.id = bb.colour ;
ORDER BY cc.id INTO CURSOR dutchflag
*!* Show sorted records
BROWSE NOMODIFY IN SCREEN
SET COLLATE TO lcCollate
 
FUNCTION RanInt() As Integer
RETURN INT(3*RAND()) + 1
ENDFUNC
</syntaxhighlight>
===Array Version===
<syntaxhighlight lang="vfp">
LOCAL i As Integer, n As Integer, colours As String, k As Integer
colours = "Red,White,Blue"
n = 15
LOCAL ARRAY balls[n,2]
*!* Make sure there is at least 1 of each colour
balls[1,1] = "Blue"
balls[1,2] = 3
balls[2,1] = "Red"
balls[2,2] = 1
balls[3,1] = "White"
balls[3,2] = 2
RAND(-1) && Initialise random number generator
FOR i = 4 TO n
k = RanInt()
balls[i,1] = GETWORDNUM(colours, k, ",")
balls[i,2] = k
ENDFOR
*!* Show the unsorted array
CLEAR
? "Unsorted..."
FOR i = 1 TO n
? balls[i,1], balls[i,2]
ENDFOR
*!* Sort the array on column 2
ASORT(balls, 2)
*!* And show it...
?
? "Sorted..."
FOR i = 1 TO n
? balls[i,1], balls[i,2]
ENDFOR
FUNCTION RanInt() As Integer
RETURN INT(3*RAND()) + 1
ENDFUNC
</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="wren">import "random" for Random
import "./sort" for Sort
 
var colors = ["Red", "White", "Blue"]
var colorMap = { "Red": 0, "White": 1, "Blue": 2 }
var colorCmp = Fn.new { |c1, c2| (colorMap[c1] - colorMap[c2]).sign }
var NUM_BALLS = 9
var r = Random.new()
var balls = List.filled(NUM_BALLS, colors[0])
 
while (true) {
for (i in 0...NUM_BALLS) balls[i] = colors[r.int(3)]
if (!Sort.isSorted(balls, colorCmp)) break
}
 
System.print("Before sorting : %(balls)")
Sort.insertion(balls, colorCmp)
System.print("After sorting : %(balls)")</syntaxhighlight>
 
{{out}}
Sample run:
<pre>
Before sorting : [Blue, Blue, White, Blue, White, Blue, Red, White, White]
After sorting : [Red, White, White, White, White, Blue, Blue, Blue, Blue]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">
def Red, White, Blue;
def Size = 10;
int A(Size), N;
 
proc ShowOrder;
[for N:= 0 to Size-1 do
case A(N) of
Red: Text(0, "Red ");
Blue: Text(0, "Blue ")
other Text(0, "White ");
CrLf(0);
];
 
proc Part3Ways; \Partition array A three ways (code from Wikipedia)
def Mid = White;
int I, J, K, T;
[I:= 0; J:= 0; K:= Size-1;
while J <= K do
if A(J) < Mid then
[T:= A(I); A(I):= A(J); A(J):= T;
I:= I+1;
J:= J+1;
]
else if A(J) > Mid then
[T:= A(J); A(J):= A(K); A(K):= T;
K:= K-1;
]
else J:= J+1;
];
 
[for N:= 0 to Size-1 do A(N):= Ran(3);
Text(0, "Original order : ");
ShowOrder;
Part3Ways;
Text(0, "Sorted order : ");
ShowOrder;
]</syntaxhighlight>
{{out}}
<pre>
Original order : Red Red Blue Blue White Red Red White Blue Red
Sorted order : Red Red Red Red Red White White Blue Blue Blue
</pre>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">const RED=0, WHITE=1, BLUE=2; var BALLS=T(RED,WHITE,BLUE);
fcn colorBalls(balls){ balls.apply(T("red","white","blue").get).concat(", "); }
 
reg balls, sortedBalls;
do{
balls=(0).pump(12,List,fcn{ BALLS[(0).random(3)] }); // create list of 12 random balls
sortedBalls=balls.sort(); // balls is read only, sort creates new list
}while(balls==sortedBalls); // make sure sort does something
println("Original ball order:\n", colorBalls(balls));
println("\nSorted ball order:\n", colorBalls(sortedBalls));</syntaxhighlight>
{{out}}
<pre>
Original ball order:
white, white, red, blue, red, red, red, red, blue, red, white, blue
 
Sorted ball order:
red, red, red, red, red, red, white, white, white, blue, blue, blue
</pre>
 
=={{header|ZX Spectrum Basic}}==
{{trans|Run_BASIC}}
<syntaxhighlight lang="zxbasic">10 LET r$="Red": LET w$="White": LET b$="Blue"
20 LET c$="RWB"
30 DIM b(10)
40 PRINT "Random:"
50 FOR n=1 TO 10
60 LET b(n)=INT (RND*3)+1
70 PRINT VAL$ (c$(b(n))+"$");" ";
80 NEXT n
90 PRINT ''"Sorted:"
100 FOR i=1 TO 3
110 FOR j=1 TO 10
120 IF b(j)=i THEN PRINT VAL$ (c$(i)+"$");" ";
130 NEXT j
140 NEXT i</syntaxhighlight>
2,044

edits