Guest User

Sorting Algorithm Visualizer in MS Quick Basic

a guest
Apr 3rd, 2023
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QBasic 7.09 KB | Source Code | 0 0
  1. DECLARE SUB quickSort (p AS INTEGER, l AS INTEGER, r AS INTEGER)
  2. DECLARE SUB colorArray (c AS INTEGER)
  3. DECLARE SUB drawArrayPos (p AS INTEGER, c AS INTEGER)
  4. DECLARE SUB bubbleSort ()
  5. DECLARE SUB populateArray ()
  6. DECLARE SUB shuffleArray ()
  7. DECLARE SUB playSoundForPos (p AS INTEGER)
  8. DECLARE SUB swapValues (p1 AS INTEGER, p2 AS INTEGER)
  9. DECLARE SUB setArrayValue (p AS INTEGER, v AS INTEGER)
  10. DECLARE FUNCTION readArrayValue% (p AS INTEGER, d AS INTEGER, s AS INTEGER)
  11. RANDOMIZE TIMER
  12. OPTION BASE 0
  13. PLAY "MB"
  14.  
  15. 'Sorting algorithm visualizer
  16. 'Written in MS Quick Basic on an IBM PC/AT
  17. 'IBM PC/AT specifications (with expansion cards):
  18. 'Intel 80286 8 MHz CPU
  19. 'EGA graphics
  20. '2 MB RAM (640K standard + 1.5 MB extended)
  21. '70 MB hard drive
  22. 'Quadtel BIOS (non-destructive upgrade from original IBM BIOS)
  23. 'IBM 5153 color monitor
  24. 'And an IBM Personal Computer Graphics Printer.
  25.  
  26. 'This program will run in EGA 320x200 16 color mode (QB SCREEN Mode 7)
  27. 'It may also run in EGA 640x200 16 color mode (QB SCREEN Mode 8)
  28. 'The IBM 5153 monitor does not support EGA 640x350 high resolution
  29. 'mode, but we don't care about that because this program
  30. 'uses entirely vertical lines, so the horizontal resolution
  31. 'matters more.
  32. 'It will visualize arbitrary algorithms for sorting arrays.
  33. 'We will need to interface with the array using QB subroutines
  34. 'in order to easily plug different algorithms into the visualizer.
  35.  
  36. 'The program will also play variable pitch sounds when an array
  37. 'item is moved from one place to another.
  38. 'We will do this with the QB SOUND command, which plays single
  39. 'channel tones through the PC Speaker, perfect for this application.
  40.  
  41. 'We will configure the array with parameters so that the
  42. 'vertical bars in the visualization will fit within the
  43. '320 or 640 pixel screen width.
  44.  
  45. DIM SHARED xres AS INTEGER, yres AS INTEGER
  46. xres = 320
  47. yres = 200
  48.  
  49.  
  50. 'Allocate memory for array:
  51. DIM SHARED arrlen AS INTEGER
  52. arrlen = VAL(COMMAND$)
  53. IF arrlen = 0 THEN arrlen = 16
  54. DIM SHARED arr(arrlen)
  55.  
  56. 'Infer some parameters from the array length and screen resolution:
  57. DIM SHARED barwidth AS INTEGER
  58. barwidth = INT(xres / arrlen)
  59. DIM SHARED barstep AS INTEGER
  60. barstep = INT(yres / arrlen)
  61. DIM SHARED reduceWidth AS INTEGER
  62. reduceWidth = 0
  63. IF barwidth > 1 THEN reduceWidth = 1
  64. IF barwidth > 2 THEN reduceWidth = 2
  65.  
  66.  
  67. 'Initialize graphics mode:
  68. IF xres = 320 AND yres = 200 THEN
  69.     SCREEN 7
  70. ELSEIF xres = 640 AND yres = 200 THEN
  71.     SCREEN 8
  72. ELSE
  73.     PRINT "Graphics mode not supported at this time."
  74. END IF
  75.  
  76.  
  77.  
  78. 'Populate the array:
  79. LOCATE 1, 1
  80. PRINT "Populating           "
  81. populateArray
  82.  
  83. 'Shuffle the array:
  84. LOCATE 1, 1
  85. PRINT "Shuffling            "
  86. shuffleArray
  87. SLEEP 1
  88.  
  89. 'We have our basic array functions working now:
  90. 'Reading, writing, populating, shuffling, swapping elements.
  91. 'Now we can write a simple bubble sort and sort the array
  92. 'after shuffling it.
  93.  
  94. LOCATE 1, 1
  95. PRINT "Bubble Sort                  "
  96. bubbleSort
  97. colorArray 2
  98. SLEEP 1
  99.  
  100. 'Shuffle the array:
  101. colorArray 15
  102. LOCATE 1, 1
  103. PRINT "Shuffling            "
  104. shuffleArray
  105. SLEEP 1
  106.  
  107. 'Now that we've gotten Bubble Sort working fairly well,
  108. 'it's time to implement a better sorting algorithm
  109. 'to demonstrate the difference: Quick Sort.
  110.  
  111. LOCATE 1, 1
  112. PRINT "Quick Sort                     "
  113. quickSort -1, 0, 0
  114. colorArray 2
  115.  
  116. WHILE INKEY$ = "": WEND
  117.  
  118. 'Return to text mode:
  119. SCREEN 0
  120. END
  121.  
  122. SUB bubbleSort
  123.  
  124.     'Sort the array using the bubble method.
  125.  
  126.    
  127.     DO
  128.         changed% = 0
  129.         FOR i% = 0 TO arrlen - 2
  130.             'Check current and next values in array:
  131.             v1% = readArrayValue(i%, 3, 1)
  132.             v2% = readArrayValue(i% + 1, 3, 1)
  133.             'Swap if needed:
  134.             IF v1% > v2% THEN swapValues i%, i% + 1: changed% = 1
  135.         NEXT i%
  136.         IF changed% = 0 THEN EXIT DO
  137.     LOOP
  138.  
  139.     'Removed recursion. What was I thinking?
  140.     'IF changed% THEN bubbleSort 'Recurse until sorted
  141.  
  142. END SUB
  143.  
  144. SUB colorArray (c AS INTEGER)
  145.  
  146.     'Color entire array a certain color
  147.  
  148.     FOR i% = 0 TO arrlen - 1
  149.         drawArrayPos i%, c
  150.         playSoundForPos (i%)
  151.     NEXT i%
  152.  
  153. END SUB
  154.  
  155. SUB drawArrayPos (p AS INTEGER, c AS INTEGER)
  156.     xpos% = p * barwidth
  157.     LINE (xpos% + reduceWidth, yres)-(xpos% + barwidth - reduceWidth, yres - arr(p)), c, BF
  158. END SUB
  159.  
  160. SUB playSoundForPos (p AS INTEGER)
  161.  
  162.     SOUND 1, 0
  163.     mult! = (p / (arrlen - 1))
  164.     SOUND (INT(mult * 780)) + 100, 1
  165.    
  166. END SUB
  167.  
  168. SUB populateArray
  169.  
  170.     'This subroutine populates the array
  171.     'with values ranging between 1 and yres
  172.  
  173.     'First bar height is 1:
  174.     h% = 1
  175.  
  176.     FOR i% = 0 TO arrlen - 1
  177.         setArrayValue i%, h%
  178.         h% = h% + barstep
  179.     NEXT i%
  180.  
  181. END SUB
  182.  
  183. SUB quickSort (p AS INTEGER, l AS INTEGER, r AS INTEGER)
  184.  
  185.     'Quick sort the array.
  186.  
  187.     'p - pivot
  188.     'l - left
  189.     'r - right
  190.  
  191.     'Choose defaults:
  192.     IF p = -1 THEN
  193.         p = INT(arrlen / 2) - 1
  194.         l = 0
  195.         r = arrlen - 1
  196.     ELSEIF p = 0 THEN
  197.         p = INT((l + r) / 2)
  198.     END IF
  199.    
  200.     i% = p
  201.     pv% = readArrayValue(p, 3, 1)
  202.     'LOCATE 1, 1
  203.     'PRINT "Left                     "
  204.     DO
  205.         i% = i% - 1
  206.         iv% = readArrayValue(i%, 3, 1)
  207.         IF iv% > pv% THEN
  208.             FOR j% = i% TO p - 1
  209.                 setArrayValue j%, readArrayValue(j% + 1, 3, 1)
  210.             NEXT j%
  211.             setArrayValue p, iv%
  212.             p = p - 1
  213.         END IF
  214.         IF i% = l THEN EXIT DO
  215.     LOOP
  216.  
  217.     i% = p
  218.     'pv% = readArrayValue(pv%, 3, 1)
  219.     'LOCATE 1, 1
  220.     'PRINT "Right                             "
  221.     DO
  222.         i% = i% + 1
  223.         iv% = readArrayValue(i%, 3, 1)
  224.         IF iv% < pv% THEN
  225.             FOR j% = i% TO p + 1 STEP -1
  226.                 setArrayValue j%, readArrayValue(j% - 1, 3, 1)
  227.             NEXT j%
  228.             setArrayValue p, iv%
  229.             p = p + 1
  230.         END IF
  231.         IF i% = r THEN EXIT DO
  232.     LOOP
  233.  
  234.     IF p > l + 1 THEN quickSort 0, l, p
  235.     IF p < r - 1 THEN quickSort 0, p, r
  236.  
  237. END SUB
  238.  
  239. FUNCTION readArrayValue% (p AS INTEGER, d AS INTEGER, s AS INTEGER)
  240.     v% = arr(p)
  241.  
  242.     IF d THEN drawArrayPos p, d
  243.     IF s THEN playSoundForPos (p)
  244.     IF d THEN drawArrayPos p, 15
  245.  
  246.     readArrayValue% = v%
  247. END FUNCTION
  248.  
  249. SUB setArrayValue (p AS INTEGER, v AS INTEGER)
  250.  
  251.     'This subroutine sets the array position p to value v
  252.  
  253.     'Erase old line:
  254.     drawArrayPos p, 0
  255.  
  256.     arr(p) = v
  257.  
  258.     'Draw new line:
  259.  
  260.     'drawArrayPos p, 4
  261.  
  262.     'playSoundForPos (p)
  263.  
  264.     drawArrayPos p, 15
  265.  
  266. END SUB
  267.  
  268. SUB shuffleArray
  269.  
  270.     'This sub randomly swaps values already in the array
  271.     'until it is statistically well shuffled enough.
  272.  
  273.     'How many times should we do this?
  274.     '7 times is too slow. Maybe just 3.
  275.  
  276.     FOR i = 1 TO 3
  277.         FOR j = 0 TO arrlen - 1
  278.             p1% = INT(arrlen * RND)
  279.             p2% = INT(arrlen * RND)
  280.             swapValues p1%, p2%
  281.         NEXT j
  282.     NEXT i
  283.  
  284. END SUB
  285.  
  286. SUB swapValues (p1 AS INTEGER, p2 AS INTEGER)
  287.  
  288.     'This subroutine swaps two values in the array.
  289.  
  290.     v1% = readArrayValue(p1, 4, 1)
  291.     setArrayValue p1, readArrayValue(p2, 4, 1)
  292.     setArrayValue p2, v1%
  293.  
  294. END SUB
  295.  
Advertisement
Add Comment
Please, Sign In to add comment