bas7ex.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
                       Example                 Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
'This example uses the SWAP statement to exchange array elements that are
'out of order. The program sorts a string array in descending order 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