The sorting numbers routines

A classic of programming required in the industrial technical schools is the realization of the so-called bubble sort : comparing two numbers so that the smallest “leaps” in the upper position. It can be realized with a pair of cycles and a temporary variable (as support ) that allows the exchange of the array values, thus avoiding the loss of one of the two numbers.
With SuperBasic to sort in ascending order we’ll proceed in this way:

240 FOR I = A-1 TO 1 STEP -1
250 FOR J = 0 TO I – 1
260 IF matrice$(J)>matrice$(J+1) THEN
270 Temp$ = matrice$(J)
280 matrice$(J) = matrice$(J+1)
290 matrice$(J+1) = Temp$
300 END IF
310 NEXT J
320 NEXT I

while the reverse order (highest to lowest ) is so made:

340 FOR I = A-1 TO 1 STEP -1
350 FOR J = 0 TO I – 1
360 IF matrice$(J)<matrice$(J+1) THEN
370 Temp$ = matrice$(J)
380 matrice$(J) = matrice$(J+1)
390 matrice$(J+1) = Temp$
400 END IF
410 NEXT J
420 NEXT I

So, the implementation on the QL is similar to what also happens with the other Basic dialects except from the END IF statement which, for example, is missing in ZX Spectrum Basic and therefore is necessary to use GOSUBs or set all on the same line. As well is missing the ELSE keyword, so we have to utilize jumps thus making the program less clear.

One thought on “The sorting numbers routines

Comments are closed.