!Simplest possible SORT program.  Take a file and sort it line by line.
!Parameters are infile/outfile.
!Offers are invited for better routines which do the same thing...
!Doesn't use the heap so currently limited to (arbitrarily) 1024 entries
!J. Butler 10/85

%include "inc:util.imp"
%begin
%string (255) %array sa(0:1023)
%integer entries,i
%string (255) infile, outfile

   %routine stringsort(%string(255)%arrayname s)
      !Sort string array data throughout its length
      %integer i,j,k,l,n1
      %string (255) temp

      n1 = 1
      l = entries - 2
      n1 = 2*n1 %while n1<l
      l = 0
       %while n1 > 1 %cycle
         n1 = n1 // 2
         i = l
          %cycle
            i = i+1
            j = i+n1
            %exitif j > entries
            k = i
             %cycle
               %exitif s(j-1) > s(k-1)
               temp = s(j-1); s(j-1) = s(k-1); s(k-1) = temp
               j = k
               k = k - n1
             %repeatuntil k < 1
          %repeat
       %repeat
    %end                 ; ! of routine STRINGSORT

%routine loadfile(%string (255) file, %string (255) %arrayname sa, %integername entries)
   %on 3,9 %start
      closeinput
      %return
   %finish

   entries=0
   openinput(1, file); selectinput(1)
   %cycle
      readline(sa(entries)); entries=entries+1
   %repeat
%end

outfile = ":t" %unless cliparam -> infile.("/").outfile
infile = ":t" %if infile = ""
loadfile(infile, sa, entries)
%stop %if entries=0
stringsort(sa)

openoutput(1, outfile); selectoutput(1)

%for i=0,1,entries-1 %cycle
   printline(sa(i))
%repeat

close output

%endofprogram
