Anonymous user
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(#)
do i=1 for #;
_.z= _.z + 1; LO=min(LO, z); HI=max(HI,
/*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:
do k=LO to HI; do x=x for _.k; @.x=k; end /*x*/
▲ end /*j*/
▲ do x=x for _.k; @.x=k; end /*x*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
|