! MExample Subprogram 11-1

! Subroutine for bubble sort algorithm.

! Sort array in ascending alphabetic order.

! Parameters: Array$() array of strings

! N no. of strings in array

 

! include main program and test with an array

! put days of the week in alphabetical order

!

 

DIM days$(7)

LET d=7

MAT READ days$

DATA su,mo,tu,we,th,fr,sa

CALL sort (days$(),d)

SET ZONEWIDTH 3 ! so the array can be printed on a line

MAT PRINT days$ ! the days of the week in alphabetical order

END

!output is: fr mo sa su th tu we

 

SUB Sort (Array$(), N)

! Bubble sort a string array

! in ascending alphabetic order.

DO

LET Sorted$ = "true"

FOR Index = 1 to (N - 1)

IF Array$(Index) > Array$(Index + 1) then

LET Temp$ = Array$(Index)

LET Array$(Index) = Array$(Index + 1)

LET Array$(Index + 1) = Temp$

LET Sorted$ = "false"

END IF

NEXT Index

LOOP until Sorted$ = "true"

END SUB