Sorting algorithms/Counting sort: Difference between revisions

m
→‎version 1: implemented an a priori minimum and maximum.
m (→‎version 1: optimized the subroutine.)
m (→‎version 1: implemented an a priori minimum and maximum.)
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(#) /* [↑] a list of some Recaman numbers.*/
m=0; _.=0 do x=x for _.k; /*M: @.x=k;max width of any number in @. end /*x*/
m=0
do i=1 for #; @.i z=word($, i); @.i=z; m=max(m, length(@.iz) ) /*get items from $ list,. */
endif i==1 /*i*/then do; LO=z; HI=z; end /*assume ··· and also findz: theLO maximum& width.HI*/
_.z= _.z + 1; LO=min(LO, z); HI=max(HI, z) /*W:find maxthe index width; M: maxLO data& widthHI.*/
end /*ji*/
/*W: max index width for the @. array*/
call show 'before sort: ' /*show the before array elements. */
say copies('▒', 55) /*show a separator line (before/after).*/
Line 2,010 ⟶ 2,012:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
countSort: procedureparse exposearg @.N; parse arg N; L=@.1; Hx=L1
do k=LO to HI; do x=x for _.k; @.x=k; end /*x*/
_.=0
do j=1 for N; end z=@.j; _.z=_.z + 1; L=min(L, z); H=max(H, z)/*k*/
end /*j*/
x=1
do k=L to H
do x=x for _.k; @.x=k; end /*x*/
end /*k*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/