DECLARE SUB HOARE (X%(), N%)
DECLARE SUB QUICK (X%(), LEFT%, RIGHT%)
DEFINT A-Z
CLS
DIM A(20)
FOR J = 0 TO 19: A(J) = INT(20 * RND): PRINT USING "####"; A(J); : NEXT J
PRINT
HOARE A(), 20
FOR J = 0 TO 19: PRINT USING "####"; A(J); : NEXT J
END
SUB HOARE (X%(), N%)
QUICK X%(), 0, N% - 1
END SUB
SUB QUICK (X%(), LEFT%, RIGHT%)
DIM I, J, XX, YY
I = LEFT%: J = RIGHT%
XX = X((LEFT% + RIGHT%) \ 2)
DO
WHILE X%(I) < XX AND I < RIGHT%: I = I + 1: WEND
WHILE XX < X%(J) AND J > LEFT%: J = J - 1: WEND
IF I <= J THEN
YY = X%(I): X%(I) = X%(J): X%(J) = YY: I = I + 1: J = J - 1
END IF
LOOP WHILE I <= J
IF LEFT% < J THEN QUICK X%(), LEFT%, J
IF I < RIGHT% THEN QUICK X%(), I, RIGHT%
END SUB