' Sort Algorithms — Bubble, Selection, Insertion sort
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    DIM nums(1 TO 8) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL temp AS LONG
    LOCAL swapped AS LONG
    LOCAL minIdx AS LONG
    LOCAL key AS LONG
    LOCAL leftIdx AS LONG
    LOCAL rightIdx AS LONG
    LOCAL n AS LONG
    LOCAL line AS STRING

    PRINT "============================"
    PRINT " Sort Algorithms"
    PRINT "============================"
    PRINT

    n = 8

    nums(1) = 42
    nums(2) = 7
    nums(3) = 99
    nums(4) = 13
    nums(5) = 55
    nums(6) = 3
    nums(7) = 88
    nums(8) = 24

    PRINT "=== BUBBLE SORT ==="
    line = ""
    FOR i = 1 TO n
        line = line & STR$(nums(i)) & " "
    NEXT i
    PRINT "Before: " & line

    FOR i = 1 TO n - 1
        swapped = 0
        FOR j = 1 TO n - i
            IF nums(j) > nums(j + 1) THEN
                temp = nums(j)
                nums(j) = nums(j + 1)
                nums(j + 1) = temp
                swapped = 1
            END IF
        NEXT j
        IF swapped = 0 THEN EXIT FOR
    NEXT i

    line = ""
    FOR i = 1 TO n
        line = line & STR$(nums(i)) & " "
    NEXT i
    PRINT "After:  " & line
    PRINT

    nums(1) = 73
    nums(2) = 14
    nums(3) = 61
    nums(4) = 28
    nums(5) = 45
    nums(6) = 91
    nums(7) = 8
    nums(8) = 57

    PRINT "=== SELECTION SORT ==="
    line = ""
    FOR i = 1 TO n
        line = line & STR$(nums(i)) & " "
    NEXT i
    PRINT "Before: " & line

    FOR i = 1 TO n - 1
        minIdx = i
        FOR j = i + 1 TO n
            IF nums(j) < nums(minIdx) THEN minIdx = j
        NEXT j
        IF minIdx <> i THEN
            temp = nums(i)
            nums(i) = nums(minIdx)
            nums(minIdx) = temp
        END IF
    NEXT i

    line = ""
    FOR i = 1 TO n
        line = line & STR$(nums(i)) & " "
    NEXT i
    PRINT "After:  " & line
    PRINT

    nums(1) = 31
    nums(2) = 12
    nums(3) = 85
    nums(4) = 47
    nums(5) = 19
    nums(6) = 63
    nums(7) = 76
    nums(8) = 52

    PRINT "=== INSERTION SORT ==="
    line = ""
    FOR i = 1 TO n
        line = line & STR$(nums(i)) & " "
    NEXT i
    PRINT "Before: " & line

    FOR i = 2 TO n
        key = nums(i)
        j = i - 1
        DO WHILE j >= 1 AND nums(j) > key
            nums(j + 1) = nums(j)
            j = j - 1
        LOOP
        nums(j + 1) = key
    NEXT i

    line = ""
    FOR i = 1 TO n
        line = line & STR$(nums(i)) & " "
    NEXT i
    PRINT "After:  " & line
    PRINT

    nums(1) = 64
    nums(2) = 34
    nums(3) = 25
    nums(4) = 12
    nums(5) = 22
    nums(6) = 11
    nums(7) = 90
    nums(8) = 18

    PRINT "=== COCKTAIL SHAKER SORT ==="
    line = ""
    FOR i = 1 TO n
        line = line & STR$(nums(i)) & " "
    NEXT i
    PRINT "Before: " & line

    leftIdx = 1
    rightIdx = n
    swapped = 1
    DO WHILE swapped
        swapped = 0
        FOR i = leftIdx TO rightIdx - 1
            IF nums(i) > nums(i + 1) THEN
                temp = nums(i)
                nums(i) = nums(i + 1)
                nums(i + 1) = temp
                swapped = 1
            END IF
        NEXT i
        IF swapped = 0 THEN EXIT DO
        rightIdx = rightIdx - 1
        swapped = 0
        FOR i = rightIdx TO leftIdx + 1 STEP -1
            IF nums(i) < nums(i - 1) THEN
                temp = nums(i)
                nums(i) = nums(i - 1)
                nums(i - 1) = temp
                swapped = 1
            END IF
        NEXT i
        leftIdx = leftIdx + 1
    LOOP

    line = ""
    FOR i = 1 TO n
        line = line & STR$(nums(i)) & " "
    NEXT i
    PRINT "After:  " & line
    PRINT

    PRINT "Sort algorithms complete."
    FUNCTION = 0
END FUNCTION
