Dutch national flag problem: Difference between revisions

no edit summary
No edit summary
Line 2,206:
Sorted: red,red,red,red,red,white,white,white,white,blue,blue,blue
</pre>
 
=={{header|Visual FoxPro}}==
===SQL Version===
<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
</lang>
===Array Version===
<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
</lang>
 
=={{header|zkl}}==
Anonymous user