DECLARE SUB SeeQSort (Array!() AS SINGLE)
FUNCTION SPC(I AS INTEGER) AS STRING
SPC = SPACE$(I)
END FUNCTION
RANDOMIZE
CONST MaxElements = 23
CONST Delay! = 1
CONST FG = 7
CONST BG = 1
CONST Hi = 15 + 16
DIM MyArray!(1 TO MaxElements) AS SINGLE
FOR X = 1 TO MaxElements
MyArray!(X) = RND * 500
NEXT
COLOR FG, BG
CLS
LOCATE 25, 1
PRINT "Press Escape to end the program early"; SPACE$(80);
CALL SeeQSort(MyArray!())
SUB SeeQSort (Array!() AS SINGLE)
DIM QStack(10) AS INTEGER
First = LBOUND(Array!)
Last = UBOUND(Array!)
DO
DO
Temp! = Array!((Last + First) \ 2)
I = First
J = Last
DO
WHILE Array!(I) < Temp!
I = I + 1
GOSUB UpdateScreen
GOSUB Pause
WEND
WHILE Array!(J) > Temp!
J = J - 1
GOSUB UpdateScreen
GOSUB Pause
WEND
IF I > J THEN
EXIT DO
END IF
IF I < J THEN
LOCATE 1, 60
COLOR BG, FG
PRINT " About to swap ";
COLOR Hi, BG
LOCATE I, 39
PRINT Array!(I);
LOCATE J, 39
PRINT Array!(J);
COLOR FG, BG
GOSUB Pause
SWAP Array!(I), Array!(J)
GOSUB UpdateScreen
LOCATE 1, 60
COLOR BG, FG
PRINT " Swapped ";
GOSUB Pause
END IF
I = I + 1
J = J - 1
LOOP UNTIL I > J
IF I < Last THEN
LOCATE 1, 60
COLOR BG, FG
PRINT " About to push ";
GOSUB Pause
QStack(StackPtr) = I
QStack(StackPtr + 1) = Last
StackPtr = StackPtr + 2
GOSUB UpdateScreen
LOCATE 1, 60
COLOR BG, FG
PRINT " Pushed ";
GOSUB Pause
END IF
Last = J
LOOP UNTIL First >= Last
IF StackPtr = 0 THEN
EXIT DO
END IF
LOCATE 1, 60
COLOR BG, FG
PRINT " About to pop ";
GOSUB Pause
StackPtr = StackPtr - 2
First = QStack(StackPtr)
Last = QStack(StackPtr + 1)
GOSUB UpdateScreen
LOCATE 1, 60
COLOR BG, FG
PRINT " Popped ";
GOSUB Pause
LOOP
COLOR FG, BG
EXIT SUB
UpdateScreen:
COLOR FG, BG
LOCATE 1, 60
PRINT SPC(15);
FOR X = 1 TO MaxElements
LOCATE X, 24
IF X = (Last + First) / 2 THEN
COLOR BG, FG
PRINT " Midpoint ==> ";
COLOR FG, BG
ELSE
PRINT SPC(14);
END IF
LOCATE X, 1
IF X = First THEN
COLOR BG, FG
PRINT " First ==> ";
COLOR FG, BG
ELSE
PRINT SPC(11);
END IF
LOCATE X, 13
IF X = Last THEN
COLOR BG, FG
PRINT " Last ==> ";
COLOR FG, BG
ELSE
PRINT SPC(11);
END IF
LOCATE X, 39
PRINT Array!(X);
PRINT SPC(17);
COLOR BG, FG
LOCATE X, 48
IF X = I THEN
PRINT " <== I ";
END IF
IF X = J THEN
LOCATE X, 56
PRINT " <== J ";
END IF
COLOR FG, BG
NEXT
RETURN
Pause:
Start! = TIMER
DO
LOOP UNTIL Start! + Delay! <= TIMER
IF INKEY$ = CHR$(27) THEN
END
END IF
RETURN
END SUB
|