Sorting algorithms/Counting sort: Difference between revisions

m
→‎version 1: optimized the subroutine.
m (→‎version 1: optimized the subroutine.)
Line 1,999:
<lang rexx>/*REXX pgm sorts an array of integers (can be negative) using the count─sort algorithm.*/
$=1 3 6 2 7 13 20 12 21 11 22 10 23 9 24 8 25 43 62 42 63 41 18 42 17 43 16 44 15 45 14 46 79 113 78 114 77 39 78 38
#=words($); w=length(#) w=length(#) /* [↑] a list of some Recaman numbers.*/
m=0
do i=1 for #; @.i=word($, i); m=max(m, length(@.i) ) /*get items from list, */
end /*i*/ /* ··· and also find the maximum width.*/
/*W: max index width; M: max data width*/
Line 2,010:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
countSort: procedure expose @.; parse arg N; L=@.1; hH=L
_.=0
do j=1 for N; z=@.j; _.z=_.z + 1; L=min(L, @.jz); h H=max(hH, @.jz)
end /*j*/
x=1
do k=L to hH
do x=x for _.k; @.x=k; end /*x*/
end /*k*/