NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 23 of 30 · Files and errorsSort values

data and read fill an array. Neighbours in the wrong order change places until nothing moves.

Programming on the NAS › BASIC in 30 lessons

23-sort.yab Download 23-sort.yab

rem Sorts numbers: neighbours in the wrong order change places
dim size(6)
for i = 1 to 6
  read size(i)
next i
data 4200, 900, 15800, 2600, 7300, 120

repeat
  swapped = 0
  for i = 1 to 5
    if size(i) < size(i + 1) then
      keep = size(i)
      size(i) = size(i + 1)
      size(i + 1) = keep
      swapped = 1
    endif
  next i
until swapped = 0

for i = 1 to 6
  print size(i), " bytes"
next i

Terminal

$ yabasic 23-sort.yab
15800 bytes
7300 bytes
4200 bytes
2600 bytes
900 bytes
120 bytes

Exercise

Sort from the smallest to the largest.