Author: Malte Bublitz
Language/File type: CBM BASIC V2
Description
Quicksort, an example for recursion in Commodore BASIC.
Source: https://www.c64-wiki.com/wiki/Recursion
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
10 GOTO 1000
196 REM
197 REM QUICKSORT - SORT SECTION OF ARRAY A$
198 REM BETWEEN LO AND HI
199 REM
200 IF LO>=HI THEN RETURN:REM DON'T SORT SINGLE ELEMENT ARRAYS
210 PI=INT(RND(1)*(HI-LO+1))+LO
220 PV=I%(PI):I%(PI)=I%(HI):I%(HI)=PV:I=LO-1:J=HI
230 I=I+1:IF A$(I%(I))<A$(PV) then 230
240 J=J-1:IF A$(I%(J))>A$(PV) AND J>LO THEN 240
250 IF I>=J THEN 280
260 T=I%(I):I%(I)=I%(J):I%(J)=T
270 GOTO 230
280 I%(HI)=I%(I):I%(I)=PV
290 RP=RP+1:RS%(RP)=HI
300 HI=I-1:GOSUB 200
310 I=HI+2:HI=RS%(RP):RP%(RP)=LO
320 LO=I:GOSUB 200
330 LO=RS%(RP):RP=RP-1:RETURN
1000 MX=500:DIM A$(MX):DIM I%(MX)
1010 DIM RS%(50):RP=0:LO=1:HI=MX:I=RND(0)
1020 OPEN 8,8,8,"0:NAMES,S,R"
1030 FOR I= 1 TO MX
1040 INPUT#8,A$(I):I%(I)=I
1050 NEXT I:CLOSE 8
1060 GOSUB 200
1070 FOR I=1 TO MX:PRINT A$(I%(I))" ";:NEXT I
1080 PRINT