Jump to content

Dutch national flag problem: Difference between revisions

Added solution for Action!
(Added 11l)
(Added solution for Action!)
Line 215:
 
RRRRRRRRWWWWWWWBBBBB, is sorted? -> X
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<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</lang>
{{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>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.