ARexx Sort
09-Dec-92 19:09:39
Sb: #30498-ARexx Sort
Fm: Frank Roberts 70357,376
To: Matthew J. W. Ratcliff 76703,1077
Arnie,
Here's a rexx shellsort routine from the latest book THE AREXX COOKBOOK
by Merrill Callaway. I highly recommend the book. Hope this routine
helps.
/* shellsort.rexx input the file for sorting */
PARSE UPPER ARG infile' 'outfile
IF infile = '' THEN DO
SAY 'Input sort filename and path: '
PARSE PULL infile
END
RC=OPEN('sortfile',infile,'READ')
IF ~RC THEN DO
SAY 'File not opened. Separate arguments with space no comma.'
EXIT
END
k=1
DO WHILE ~EOF('sortfile')
list.k=READLN('sortfile')
k=k+1
END
listlength = k-2
SAY listlength 'entries to sort…'
/* The Shell Sort */
RC=TIME('R')
span = 1
DO WHILE (span < listlength); span = span * 2; END
DO WHILE (span > 1)
span = span % 2
numpairs = listlength – span
DO node = 1 TO numpairs
nextnode = node + span
IF list.node > list.nextnode THEN
DO
store = list.nextnode
list.nextnode = list.node
DO bubpos = node-span TO 1 BY -span WHILE (store < list.bubpos)
nextnode = bubpos + span
list.nextnode = list.bubpos
END bubpos
bubpos = bubpos + span
list.bubpos = store
END
END node
END
SAY 'Elapsed time='TIME('E')' seconds.'
/* output results */
IF outfile = '' THEN DO
SAY 'Specify filename and path for sorted output.'
PARSE PULL outfile
IF outfile='' THEN DO
SAY 'No output written.'
EXIT 5
END
END
CALL OPEN('outfile',outfile,'WRITE')
DO i=1 TO listlength
CALL WRITELN('outfile',list.i)
END i
SAY 'Output has been written to 'outfile
EXIT 0