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 -1250 FOR J = 0 TO I – 1260 IF matrice$(J)>matrice$(J+1) THEN270 Temp$ = matrice$(J)280 matrice$(J) = matrice$(J+1)290 matrice$(J+1) = Temp$300 END IF310 NEXT J320 NEXT I
while the reverse order (highest to lowest ) is so made:
340 FOR I = A-1 TO 1 STEP -1350 FOR J = 0 TO I – 1360 IF matrice$(J)<matrice$(J+1) THEN370 Temp$ = matrice$(J)380 matrice$(J) = matrice$(J+1)390 matrice$(J+1) = Temp$400 END IF410 NEXT J420 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”
Keep up the good work!
Comments are closed.