Jump to content

Sorting algorithms/Counting sort: Difference between revisions

m
→‎version 1: added/changed whitespace and comments.
m (→‎version 1: added/changed whitespace and comments.)
Line 1,778:
Negative, zero, and positive integers are supported.
===version 1===
<lang rexx>/*REXX program sorts an array 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 ji=1 for #
@.ji=word($,ji) /*assignget a Recaman # from a list.*/
end /*j*/ /* [↑] end assign 40 Recaman numbers/*i*/
call show@ 'before sort: ' /*show the before array elements. */
callsay countSort copies('▒',55) # /*show a pretty separator line. /*sort # entries of the @. array.*/
call show@countSort # ' after sort: ' /*showsort thea number afterof arrayentries elementsof @. array.*/
exitcall show ' after sort: ' /*show the after array elements. /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────COUNTSORT subroutine────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
countSort: procedure expose @.; parse arg N; L=@.1; h=L; _.=0; x=1
countSort: procedure expose do@.; j=1 for parse arg N; z=@.j; _.z=_.z+1; L=min(L,@.j)1; h=max(h,@L; _.j)=0; end /*j*/x=1
do k=L to do h;j=1 for N; do xz=x@.j; for _.kz=_.z+1; L=min(L,@.x=kj); end /*x*/h=max(h,@.j); end /*kj*/
do k=L to h; do x=x for _.k; @.x=k; end /*x*/; end /*k*/
return
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: do s=1 for #; say right("element",20) right(s,length(#)w) arg(1) @.s; end; return</lang>
end /*s*/
say copies('▒',55) /*show a pretty separator line. */
return</lang>
'''output'''
<pre style="height:50ex">
Line 1,880 ⟶ 1,878:
element 39 after sort: 113
element 40 after sort: 114
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.