Search code examples
arrayssortingbubble-sortcommodore

Bubble sort an array of strings in commodore 4.0 basic?


Working on a bubble sort for an array of strings in commodore 4.0 basic; can anyone help me out?

I have the 'main' part of the algorithim so far but I am confused on how to make it a subroutine because how would I make the array of strings a parameter so that I can call upon it afterwards?

rem: s = holder value
for x = 0 to (length of array with strings)
if array(x)<(array(x+1)) then array(x+1)=s
then array(x+1)=array(x)
then array(x)=s
next
end

Solution

  • To do a bubble sort, you have to loop over the data in a number of passes, and in each pass keep track of whether any swaps have been made. Once you finish a pass with zero swaps made, you know the array has been sorted. In addition, your swapping logic is faulty; you'd have to store array(x+1) in s, then copy array(x) to array(x+1), then finally put array(s) into array(x).

    I'm not really sure what you mean by "How would I make the array of strings a parameter so that I can call upon it afterwards?" Commodore BASIC doesn't have user-defined functions* like most languages, so "parameter" isn't a very useful concept. Mostly people would store the value(s) to pass to a subroutine in variables (all variables are global), and then the subroutine would do likewise to return a value or values (if it returned anything). It's up to the programmer to keep track of what variables are used for what purposes, as well as what line numbers subroutines start on (and where conditional branches and loop starting points are located).

    Here is a version I whipped up, in a format as "structured" as I could manage in BASIC 2.0; it should work as is in 4.0:

    100 gosub 150:rem set up array
    110 gosub 230:rem sort array
    120 gosub 420:rem print array
    130 end
    140 :
    150 rem set up array
    160 rem l=length of array
    170 l=10
    180 dim ar(l)
    190 for i=0 to l-1:read ar(i):next
    200 data 5,1,4,2,8,7,3,6,9,10
    210 return
    220 :
    230 rem sort array
    240 if l<2 then return
    250 rem else
    260 : rem sw=whether this pass has had any swaps
    270 : rem start of pass
    280 :   sw=0
    290 :   for x=0 to l-2
    300 :     if ar(x)<=ar(x+1) then goto 380
    310 :     rem else
    320 :       rem s=placeholder value
    330 :       s=ar(x+1)
    340 :       ar(x+1)=ar(x)
    350 :       ar(x)=s
    360 :       sw=-1
    370 :     rem endif
    380 :   next
    390 : if sw then goto 270
    400 return
    410 :
    420 rem print array
    430 if l=0 then print "(empty array)":return
    440 rem else
    450 : for i=0 to l-1:print ar(i);:next
    460 : print
    470 return
    

    Note that : at the beginning of a line just allows us to insert lines that are otherwise blank, or indent what follows on the line; if you tried putting extra spaces at the start of a line, the BASIC editor would just remove them later. This just makes things look a little more like a modern programming language.

    * There is a way to define functions which take one floating-point parameter and return one floating-point result, but the definition has to be all on one line and can't use any conditionals, loops, etc. It's good for simple mathematical formulae. E.g.:

    10 def fn f(x)=x*1.5
    20 print f(3)
    

    prints " 4.5".