qb45advr.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
SWAP Statement Programming Example
  QuickSCREEN      Details     Example      Contents      Index
──────────────────────────────────────────────────────────────────────────────
SWAP Statement Programming Example
 
The following program sorts the elements of a string array in descending
order using a Shell sort. It uses SWAP to exchange array elements that are
out of order.
 
' Sort the word list using a Shell sort.
Num% = 4
Array$(1) = "New York"
Array$(2) = "Boston"
Array$(3) = "Chicago"
Array$(4) = "Seattle"
Span% = Num%  2
DO WHILE Span% > 0
    FOR I% = Span% TO Num% - 1
    J% = I% - Span% + 1
        FOR J% = (I% - Span% + 1) TO 1 STEP -Span%
            IF Array$(J%) <= Array$(J% + Span%) THEN EXIT FOR
            '
            ' Swap array elements that are out of order.
            '
            SWAP Array$(J%), Array$(J% + Span%)
         NEXT J%
    NEXT I%
    Span% = Span%  2
LOOP
CLS
FOR I% = 1 TO Num%
    PRINT Array$(I%)
NEXT I%
END
 
This program could be converted into a useful SUB, which would sort a
string array of any size, by using the SUB...END SUB statements as shown
below:
 
' Sort the word list using a Shell sort.
SUB ShellSort (Array$(), Num%) STATIC
   Span% = Num%  2
   DO WHILE Span% > 0
      FOR I% = Span% TO Num% - 1
         J% = I% - Span% + 1
         FOR J% = (I% - Span% + 1) TO 1 STEP -Span%
            IF Array$(J%) <= Array$(J% + Span%) THEN EXIT FOR
            ' Swap array elements that are out of order.
            SWAP Array$(J%), Array$(J% + Span%)
         NEXT J%
      NEXT I%
      Span% = Span%  2
   LOOP
END SUB