Bob’s Notebook: Quicksort

See all articles from Sinc-Link v3 n6
Author(s): Bob Mitchell
Publication: Sinc-Link
Pub Details: Sinc-Link v3 n6 (Volume 3, Issue 6)
Date: November/December 1985
Pages: 10

QUICKSORT SUBROUTINE

This is the first of a two part series which will provide the reader with a fast method of sorting strings and then a fast method of searching the strings for particular data.

The ‘Quicksort subroutine was developed from material contained in an article in Computing Now June 1983 titled Quicksort in Basic by John W. Ross.

The Quicksort algorithm, invented by C.A.R. Hoare, uses a divide and conquer technique to divide a list into successively smaller parts.

There is one main shortcoming: When trying to re-sort a list which is already almost completely sorted (eg. when a record is added to a sorted list), Quicksort will seem to hang up. To avoid this, try sorting on another field first, then sort on the desired field.

Expect Quicksort to be about 12 times faster than Bubble Sort for n=200.

To use this subroutine with your data list, include lines in the main program that set the variable n to equal the size of the list so far. In this example, r$ may be dimensioned to 200,32; whereas the entries so far might only be as far as 100. Therefore, LET n=100. If r$ is broken into fields, you will want to sort on one or other of these fields: say field 1 starting at 1 and ending at 10; therefore LET a=1: LET b=10. Then include a line GO SUB 9500.

In the second part, I shall provide a Binary Search Routine which will depend on the list already having been sorted by whatever field you wish to search on.

The variables used in these subroutines are chosen so as to avoid clashing with variables likely to have been used in your main programs. However, if there is any duplication, you will have to make suitable modifications.

Bob Mitchell
October 1985

9500 REM QUICKSORT
9505 CLS
9510 DIM l(20): DIM h(20)
9555 REM START OF SUBROUTINE
9560 LET l(1)=1
9565 LET h(1)=n
9570 LET z0=2
9575 IF z0<=1 THEN RETURN
9580 IF l(z0)>=h(z0) THEN LET z0=z0-1
9585 IF l(z0)>=h(z0) THEN GO TO 9575
9590 LET p0=l(z0)-1
9595 LET q0=h(z0)
9600 LET y0=q0
9605 IF p0>=q0 THEN GO TO 9650
9610 LET p0=p0+1
9615 IF r$(p0,a TO b)<r$(y0,a TO b) THEN GO TO 9610
9620 LET q0=q0-1
9625 IF q0>1 THEN IF r$(q0,a TO b)>r$(y0,a TO b) THEN GO TO 9620
9630 IF p0<q0 THEN LET w$=r$(p0)
9635 IF p0<q0 THEN LET r$(p0)=r$(q0)
9640 IF p0<q0 THEN LET r$(q0)=w$
9645 GO TO 9605
9650 LET q0=h(z0): LET w$=r$(p0): LET r$(p0)=r$(q0): LET r$(q0)=w$
9655 IF p0-l(z0)<h(z0)-1 THEN LET l(z0+1)=l(z0): LET h(z0+1)=p0-1: LET l(z0)=p0+1: GO TO 9675
9660 LET l(z0+1)=p0+1
9665 LET h(z0+1)=h(z0)
9670 LET h(z0)=p0-1
9675 LET z0=z0+1
9680 GO TO 9575

TITLE LINE IN A DISPLAY

Sometimes, when presenting data on the screen, you may have wished that you could keep the top line from scrolling off the top (eg, a table with headings across the top).

Here is a short machine code routine which will do the trick.

I have located it at 60000 but you may put it anywhere:

EA60 0617   LD B,17
EA62 CD7F09 CALL 097F
EA65 C9 RET

If you want to keep the two top lines from scrolling, change 17 to 16 at EA61. Call the routine by RANDOMIZE USR 60000.

Example:

9960 PRINT AT 0,0;"ADDRESS     CONTENTS   CHARACTER "
9970 FOR I=0 TO 1000
9972 IF PEEK I<32 THEN GO TO 9976
9974 PRINT I;TAB 12;PEEK I;TAB 24;CHR$ PEEK I; TAB 32
9975 IF PEEK 23659=2 THEN RANDOMIZE USR 60000: PRINT AT 1,0;
9976 NEXT I

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.