Classic Computer Magazine Archive COMPUTE! ISSUE 54 / NOVEMBER 1984 / PAGE 10

A BASIC Sort

My daughter has written an inventory program to list our music cassettes. It uses DATA statements to list type of music, name of cassette, and performer. We have for several months attempted to write a routine whereby we can list all the performers in alphabetical order, but without success. Is there any way we can do this and not have the program running forever?

Don Cordry

There are a number of good, fast sorts, but the bubble sort is one of the shortest and easiest to understand and modify. It works by comparing every item to the one beneath it. If the two items are out of order, they are switched. The sort continues until no more exchanges are necessary.

The name comes from the way lower-ranked data tends to "bubble" upwards. The small subroutine below can be used to sort string arrays. It's easy to modify for whatever purpose you need. The variable N should be set to the number of performers, and all the performers should be read into the array prior to the sort. This program will work as is with most versions of BASIC, but would need to be modified to run on an Atari.

5000 EX = 0
5010 FORI = 1 TON - 1
5020 IPA$(I) > A$(I + 1) THEN T$ = A$(I) : A$(I) =
      A$(I + 1) : A$ (I + 1) = T$ : EX = 1 
5030 NEXT I 
5040 IF EX < > 0 THEN 5000 
5050 RETURN