Sorting algorithms/Radix sort: Difference between revisions
Content added Content deleted
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: | Line 2,386: | ||
call gen /*call subroutine to generate numbers. */ |
call gen /*call subroutine to generate numbers. */ |
||
call radSort n /*invoke the radix sort subroutine. */ |
call radSort n /*invoke the radix sort subroutine. */ |
||
call show /*display the elements in the @ array*/ |
|||
⚫ | |||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 2,401: | Line 2,400: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
radSort: procedure expose @. w; parse arg size; mote= c2d(' '); #= 1; !.#._n= size |
radSort: procedure expose @. w; parse arg size; mote= c2d(' '); #= 1; !.#._n= size |
||
!.#._b=1 |
!.#._b= 1 |
||
!.#._i=1; do i=1 for size; y=@.i; |
!.#._i= 1; do i=1 for size; y=@.i; @.i= right(abs(y), w, 0); if y<0 then @.i= '-'@.i |
||
end /*i*/ |
end /*i*/ /* [↑] negative case.*/ |
||
do while #\==0; |
do while #\==0; ctr.= 0; L= 'ffff'x; low= !.#._b; n= !.#._n; $= !.#._i; H= |
||
#=#-1 |
#= #-1 /* [↑] is the radix. */ |
||
do j=low for n; parse var @.j =($) _ +1; ctr._=ctr._ + 1 |
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=_ |
if ctr._==1 & _\=='' then do; if _<<L then L=_; if _>>H then H=_ |
||
end /* ↑↑ */ |
end /* ↑↑ */ |
||
Line 2,417: | Line 2,416: | ||
L= c2d(L); H= c2d(H); ?= ctr._ + low; top._= ?; ts= mote |
L= c2d(L); H= c2d(H); ?= ctr._ + low; top._= ?; ts= mote |
||
max= L |
max= L |
||
do k=L to H; _= d2c(k,1); c= ctr._ |
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._= ? |
if c>ts then parse value c k with ts max; ?= ?+c; top._= ? |
||
end /*k*/ |
end /*k*/ |
||
Line 2,426: | Line 2,425: | ||
if piv>=c then leave; top._= c; ?= @.c; @.c= it; it= ? |
if piv>=c then leave; top._= c; ?= @.c; @.c= it; it= ? |
||
end /*forever*/ |
end /*forever*/ |
||
top._= piv; |
top._= piv; @.piv= it; piv= piv + ctr._ |
||
end /*while piv<low+n */ |
end /*while piv<low+n */ |
||
i= max |
i= max |
||
Line 2,442: | Line 2,441: | ||
end /*while #\==0 */ |
end /*while #\==0 */ |
||
#= 0 /* [↓↓↓] handle neg. and pos. arrays. */ |
#= 0 /* [↓↓↓] handle neg. and pos. arrays. */ |
||
do i=size by -1 |
do i=size by -1 for size; if @.i>=0 then iterate; #= #+1; @@.#= @.i |
||
end /*i*/ |
end /*i*/ |
||
do j=1 for size; if @.j>=0 then do; #= #+1; @@.#= @.j; end; @.j= @@.j+0 |
do j=1 for size; if @.j>=0 then do; #= #+1; @@.#= @.j; end; @.j= @@.j+0 |
||
end /*j*/ |
end /*j*/ /* [↑↑↑] combine 2 lists into 1 list. */ |
||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
show: do j=1 for n; say 'item' right(j, w) "after the radix sort:" right(@.j, w) |
|||
⚫ | |||
{{out|output|text= (with the middle section elided.)}} |
{{out|output|text= (with the middle section elided.)}} |
||