Sorting algorithms/Counting sort: Difference between revisions
Content added Content deleted
m (→version 1: added/changed whitespace and comments, simplified and optimized a DO loop..) |
|||
Line 1,999: | Line 1,999: | ||
<lang rexx>/*REXX pgm sorts an array of integers (can be negative) using the count─sort algorithm.*/ |
<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 |
$=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($); |
#= words($); w= length(#); _.= 0 /* [↑] a list of some Recaman numbers.*/ |
||
m=0; |
m= 0; LO= word($, 1); HI= LO /*M: max width of any number in @. */ |
||
do i=1 for #; |
do i=1 for #; z= word($, i); @.i= z; m= max(m, length(z)) /*get from $ list. */ |
||
_.z= _.z + 1; LO= min(LO, z); HI= max(HI, z) /*find the LO & HI.*/ |
|||
_.z= _.z + 1; LO=min(LO, z); HI=max(HI, z) /*find the LO & HI.*/ |
|||
end /*i*/ |
end /*i*/ |
||
/*W: max index width for the @. array*/ |
/*W: max index width for the @. array*/ |
||
Line 2,012: | Line 2,011: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
countSort: parse arg N; |
countSort: parse arg N; x= 1; do k=LO to HI; do x=x for _.k; @.x= k; end /*x*/ |
||
end /*k*/ |
|||
end /*k*/ |
|||
return |
return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |