Sorting algorithms/Counting sort: Difference between revisions

m
→‎version 1: added whitespace, changed comments and indentations.
m (Added Sidef language.)
m (→‎version 1: added whitespace, changed comments and indentations.)
Line 1,500:
=={{header|REXX}}==
===version 1===
<lang rexx>/*REXX program sorts an array using the count-sort method algorithm. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call countSort N# /*sort N entries of the @. array.*/
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────countSort──────────────────────────────────COUNTSORT subroutine────────────────*/
countSort: procedure expose @.; parse arg N; h=@.1; L=h
 
do i=2 to N; L=min(L,@.i); h=max(h,@.i)
end /*i*/
 
_.=0; do j=1 for N; x=@.j; _.x=_.x+1
end /*j*/
 
#x=1; do k=L to h; if _.k\==0 then do #x=#x for _.k
@.#x=k
end /*#x*/
end /*k*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.$=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 /*assign14 46 4079 113 Recaman78 114 numbers.77 39 */78 38
#=words($)
@.1 = 1 ; @.9 = 21 ; @.17= 25 ; @.25= 17 ; @.33= 79
@.2 = 3 ; @.10= 11do ;j=1 for # @.18= 43 ; @.26= 43 ; /* [↓] assign 40 @Recaman numbers.34= 113*/
@.3 = 6 ; @.11j=word($,j) 22 ; @.19= 62 ; /*assign a @.27=Recaman # 16from ;a @list.35= 78*/
@.4 = 2 ; @.12= 10end ; @.20= 42 ; @.28= 44 ; @.36= 114/*j*/
@.5 = 7 ; @.13= 23 ; @.21= 63 ; @.29= 15 ; @.37= 77
@.6 = 13 ; @.14= 9 ; @.22= 41 ; @.30= 45 ; @.38= 39
@.7 = 20 ; @.15= 24 ; @.23= 18 ; @.31= 14 ; @.39= 78
@.8 = 12 ; @.16= 8 ; @.24= 42 ; @.32= 46 ; @.40= 38
 
do N=1 while @.N\==''; end /*determine the number of entries*/
N=N-1 /*adjust highItem slightly. */
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: do s=1 for #
show@: widthH=length(N) /*max width of any element number*/
pad= say left('',9); "element" right(s,length(#)) arg(1)': do' s=1 for N@.s
end /*s*/
say pad 'element' right(s,widthH) arg(1)": " @.s
 
end /*s*/
say copies('─',4050) /*show a pretty separator line. */
return</lang>
'''output'''
Line 1,626 ⟶ 1,619:
────────────────────────────────────────
</pre>
 
===version 2===
{{trans|PL/I}}