Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REM
- REM shellsort.bas
- REM
- REM illustrate the usage of arrays
- REM in HiSOFT/MaxonBASIC OR QBasic/QB64
- REM
- DEFINT A-Z
- REM define arrays
- DIM UnsortedList$(10) ' holds unsorted characters
- DIM SortedList$(10)
- REM DECLARE functions AND sub-programs
- DECLARE SUB Shell_Sort(Words$(1))
- REM -- Program work starts here --
- MainProgram:
- CLS
- PRINT "ShellSort in Basic"
- PRINT "==================": PRINT
- REM -- Fill array --
- UnsortedList$(0) = "9"
- UnsortedList$(1) = "3"
- UnsortedList$(2) = "5"
- UnsortedList$(3) = "4"
- UnsortedList$(4) = "7"
- UnsortedList$(5) = "1"
- UnsortedList$(6) = "8"
- UnsortedList$(7) = "2"
- UnsortedList$(8) = "6"
- UnsortedList$(9) = "0"
- REM -- PRINT unsorted array --
- PRINT "Unsorted List: ";
- FOR i = 0 TO (UBOUND(UnsortedList$, 1))
- PRINT UnsortedList$(i);
- NEXT i
- PRINT
- REM -- Shell-sort the LIST AND copy it into a new array --
- FOR i = 0 TO (UBOUND(UnsortedList$, 1))
- CALL Shell_Sort(UnsortedList$())
- SortedList$(i) = UnsortedList$(i) ' also copy content to 2nd array!
- PRINT UnsortedList$(i)
- NEXT i
- REM -- PRINT sorted array --
- PRINT "Sorted List: ";
- FOR i = 0 TO (UBOUND(UnsortedList$, 1))
- PRINT UnsortedList$(i);
- NEXT i
- PRINT
- REM -- PRINT sorted array --
- PRINT "New Sorted List: ";
- FOR i = 0 TO (UBOUND(SortedList$, 1))
- PRINT SortedList$(i);
- NEXT i
- PRINT
- Rem -- ShellSort --
- SUB Shell_Sort (Words$( 1))
- STATIC HowFAR, Top, i, j
- Top = UBOUND(Words$)
- HowFAR = Top \ 2
- DO WHILE HowFAR > 0
- FOR i = HowFAR TO Top - 1
- j = i - HowFAR + 1
- FOR j = (i - HowFAR + 1) TO 1 STEP - HowFAR
- IF Words$(j) <= Words$(j + HowFAR) THEN
- EXIT FOR
- END IF
- SWAP Words$(j), Words$(j + HowFAR)
- NEXT j
- NEXT i
- HowFAR = HowFAR \ 2
- LOOP
- END SUB
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement