Advertisement
micha_b

ShellSort.bas

Nov 9th, 2022
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QBasic 1.97 KB | Fixit | 0 0
  1. REM
  2. REM shellsort.bas
  3. REM
  4. REM illustrate the usage of arrays
  5. REM in HiSOFT/MaxonBASIC OR QBasic/QB64
  6. REM
  7.  
  8. DEFINT A-Z
  9.  
  10. REM define arrays
  11. DIM UnsortedList$(10) ' holds unsorted characters
  12. DIM SortedList$(10)
  13.  
  14. REM DECLARE functions AND sub-programs
  15. DECLARE SUB Shell_Sort(Words$(1))
  16.  
  17. REM -- Program work starts here --
  18. MainProgram:
  19. CLS
  20. PRINT "ShellSort in Basic"
  21. PRINT "==================": PRINT
  22.  
  23. REM -- Fill array --
  24. UnsortedList$(0) = "9"
  25. UnsortedList$(1) = "3"
  26. UnsortedList$(2) = "5"
  27. UnsortedList$(3) = "4"
  28. UnsortedList$(4) = "7"
  29. UnsortedList$(5) = "1"
  30. UnsortedList$(6) = "8"
  31. UnsortedList$(7) = "2"
  32. UnsortedList$(8) = "6"
  33. UnsortedList$(9) = "0"
  34.  
  35. REM -- PRINT unsorted array --
  36. PRINT "Unsorted List: ";
  37. FOR i = 0 TO (UBOUND(UnsortedList$, 1))
  38.     PRINT UnsortedList$(i);
  39.     NEXT i
  40.     PRINT
  41.    
  42. REM -- Shell-sort the LIST AND copy it into a new array --
  43. FOR i = 0 TO (UBOUND(UnsortedList$, 1))
  44.     CALL Shell_Sort(UnsortedList$())
  45.     SortedList$(i) = UnsortedList$(i) ' also copy content to 2nd array!
  46.     PRINT UnsortedList$(i)
  47. NEXT i
  48.  
  49. REM -- PRINT sorted array --
  50. PRINT "Sorted List: ";
  51. FOR i = 0 TO (UBOUND(UnsortedList$, 1))
  52.     PRINT UnsortedList$(i);
  53. NEXT i
  54. PRINT
  55.    
  56. REM -- PRINT sorted array --
  57. PRINT "New Sorted List: ";
  58. FOR i = 0 TO (UBOUND(SortedList$, 1))
  59.     PRINT SortedList$(i);
  60. NEXT i
  61. PRINT
  62.    
  63. Rem -- ShellSort --
  64. SUB Shell_Sort (Words$( 1))
  65.     STATIC HowFAR, Top, i, j
  66.     Top = UBOUND(Words$)
  67.     HowFAR = Top \ 2
  68.    
  69.     DO WHILE HowFAR > 0
  70.         FOR i = HowFAR TO Top - 1      
  71.             j = i - HowFAR + 1
  72.             FOR j = (i - HowFAR + 1) TO 1 STEP - HowFAR            
  73.                 IF Words$(j) <= Words$(j + HowFAR) THEN                
  74.                     EXIT FOR                    
  75.                 END IF                
  76.             SWAP Words$(j), Words$(j + HowFAR)                
  77.             NEXT j            
  78.         NEXT i
  79.                
  80.         HowFAR = HowFAR \ 2
  81.     LOOP    
  82. END SUB
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement