Personal Computer News


Bubblesorting On The Spectrum

 
Published in Personal Computer News #091

Sorting arrays on the Spectrum can be time consuming, but try Stuart Nicholls' machine code program and you'll have put your foot on the accelerator.

Blowing Bubbles

Sorting arrays on the Spectrum can be time consuming, but try Stuart Nicholls' machine code program and you'll have put your foot on the accelerator

Anyone who has written Spectrum programs which require sorting words or numbers will tell you Basic is extremely slow - especially if you are using a standard bubble sort.

I have produced a machine code routine that can give speed increases in the order of 100 times for word sorts and 12 times for number sorts. What's more, it is user-friendly and intended for use by Basic programmers.

The machine code routines can be used to sort *any* one-dimensional numeric array and two-dimensional string array. In other words, you are not limited to using a specific letter for your array. Indeed, you may have several numeric and string arrays in your program and still sort each one in turn with the same routine. Any number of elements in the chosen array can be sorted in either ascending or descending order.

Defining the paramters of the required sort is extremely simple as the machine code routines use Basic variables to hold this information. For example, if a string array X$(100,50) is to be sorted, but only the first 75 elements are required in ascending order, this would be set up with these two lines:

   LET Q=75:LET Q$="X$"
   LET SORT=USR 64000

Similarly, if all of a numeric array N(1000) is to be sorted in descending order, this would be set up as:

   LET Q=-1000:LET Q$="N"
   LET SORT=USR 64000

The variable Q is used to hold the number of elements to be sorted; a positive value indicates an ascending sort whereas a negative value indicates that a descending sort is required. The string variable Q$ is used to hold the array to be sorted.

The routine is fully error-trapped and gives the normal error reports if Q or Q$ is not defined, is defined incorrectly or an array correctly defined has not been dimensioned. You may also exit the routine at any point by pressing space.

The value of Q must be at least 2, as any value btween -1 and 1 gives a parameter error report. Decimal values, should they be set up by mistake, are rounded up to the nearest whole number. Finally, if the value of Q is greater than the number of elements as defined by Q$, it is assumed that the whole array is to be sorted.

The hexdump (Listing 1) is for the 48K Spectrum and starts at address 64000, the routine being 532 bytes long. The checksum given at the end of each line is the sum of the previous eight bytes MOD 256.

Should anyone wish to assemble the code elsewhere I have included my assembly listing (Listing 2). Machine language programmers should note that the code is self-modifying to reduce the length of the routine and ascending and descending sorts share the same subroutines.

Array() Elements Typical sort time (secs)
M/code Shell Bubble
10 0.2 1.8 1.2
20 0.45 4.5 5.0
50 2.5 14.5 31.0
100 8.0 34.0 120.0
300 80.0 145.0 1106.0
Table 1
Array$() Elements Typical sort time (secs)
M/code Shell Bubble
10,10 0.06 2.0 1.5
20,10 0.08 4.5 5.0
50,10 0.35 15.5 35.0
100,10 1.00 36.0 140.0
300,10 10.50 155.0 1240.0
Table 2

Tables 1 and 2 compare the speed of the machine code routine with a Basic shell sort and bubble sort for random number and word arrays of various dimensions. The timings are an average taken over four different 'random' arrays and were obtained using the system variable 'frames' to give values before and after the sort.

Stuart Nicholls