Sorting algorithms/Radix sort: Difference between revisions

m
→‎{{header|REXX}}: added a subroutine, optimized the sort algorithm, add/changed comments and whitespace.
m (→‎{{header|Groovy}}: elided style from PRE tag.)
m (→‎{{header|REXX}}: added a subroutine, optimized the sort algorithm, add/changed comments and whitespace.)
Line 2,386:
call gen /*call subroutine to generate numbers. */
call radSort n /*invoke the radix sort subroutine. */
call show do j=1 for n; say 'item' right(j, w) "after the radix sort:" right( /*display the elements in the @.j, w) array*/
end /*j*/ /* [↑] display sorted items ───► term.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,401 ⟶ 2,400:
/*──────────────────────────────────────────────────────────────────────────────────────*/
radSort: procedure expose @. w; parse arg size; mote= c2d(' '); #= 1; !.#._n= size
!.#._b= 1;
!.#._i= 1; do i=1 for size; y=@.i; @.i= right(abs(y), w, 0); if y<0 then @.i= '-'@.i
end /*i*/ /* [↑] negative case.*/
 
do while #\==0; ctr.= 0; L= 'ffff'x; low= !.#._b; n= !.#._n; $= !.#._i; H=
#= #-1 /* [↑] is the radix. */
do j=low for n; parse var @.j =($) _ +1; ctr._= ctr._ + 1
if ctr._==1 & _\=='' then do; if _<<L then L=_; if _>>H then H=_
end /* ↑↑ */
Line 2,417 ⟶ 2,416:
L= c2d(L); H= c2d(H); ?= ctr._ + low; top._= ?; ts= mote
max= L
do k=L to H; _= d2c(k, 1); c= ctr._ /* [↓] swap 2 item radices.*/
if c>ts then parse value c k with ts max; ?= ?+c; top._= ?
end /*k*/
Line 2,426 ⟶ 2,425:
if piv>=c then leave; top._= c; ?= @.c; @.c= it; it= ?
end /*forever*/
top._= piv; @.piv= it; piv= piv + ctr._
end /*while piv<low+n */
i= max
Line 2,442 ⟶ 2,441:
end /*while #\==0 */
#= 0 /* [↓↓↓] handle neg. and pos. arrays. */
do i=size by -1 tofor 1size; if @.i>=0 then iterate; #= #+1; @@.#= @.i
end /*i*/
do j=1 for size; if @.j>=0 then do; #= #+1; @@.#= @.j; end; @.j= @@.j+0
end /*j*/; return /* [↑↑↑] combine 2 lists into 1 list. */</lang>
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for n; say 'item' right(j, w) "after the radix sort:" right(@.j, w)
end /*j*/; return /* [↑] display sorted items ───► term.*/</lang>
{{out|output|text=&nbsp; &nbsp; (with the middle section elided.)}}