Sorting algorithms/Counting sort: Difference between revisions

Content added Content deleted
m (Added Sidef language.)
m (→‎version 1: added whitespace, changed comments and indentations.)
Line 1,500: Line 1,500:
=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
===version 1===
<lang rexx>/*REXX program sorts an array using the count-sort method. */
<lang rexx>/*REXX program sorts an array using the count-sort algorithm. */
call gen@ /*generate the array elements. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call show@ 'before sort' /*show the before array elements.*/
call countSort N /*sort N entries of the @. array.*/
call countSort # /*sort N entries of the @. array.*/
call show@ ' after sort' /*show the after array elements.*/
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────countSort subroutine────────────────*/
/*──────────────────────────────────COUNTSORT subroutine────────────────*/
countSort: procedure expose @.; parse arg N; h=@.1; L=h
countSort: procedure expose @.; parse arg N; h=@.1; L=h


do i=2 to N; L=min(L,@.i); h=max(h,@.i)
do i=2 to N; L=min(L,@.i); h=max(h,@.i)
end /*i*/
end /*i*/


_.=0; do j=1 for N; x=@.j; _.x=_.x+1
_.=0; do j=1 for N; x=@.j; _.x=_.x+1
end /*j*/
end /*j*/


#=1; do k=L to h; if _.k\==0 then do #=# for _.k
x=1; do k=L to h; if _.k\==0 then do x=x for _.k
@.#=k
@.x=k
end /*#*/
end /*x*/
end /*k*/
end /*k*/
return
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.= /*assign 40 Recaman numbers. */
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 14 46 79 113 78 114 77 39 78 38
#=words($)
@.1 = 1 ; @.9 = 21 ; @.17= 25 ; @.25= 17 ; @.33= 79
@.2 = 3 ; @.10= 11 ; @.18= 43 ; @.26= 43 ; @.34= 113
do j=1 for # /* [↓] assign 40 Recaman numbers.*/
@.3 = 6 ; @.11= 22 ; @.19= 62 ; @.27= 16 ; @.35= 78
@.j=word($,j) /*assign a Recaman # from a list.*/
@.4 = 2 ; @.12= 10 ; @.20= 42 ; @.28= 44 ; @.36= 114
end /*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
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: do s=1 for #
show@: widthH=length(N) /*max width of any element number*/
pad=left('',9); do s=1 for N
say left('',9) "element" right(s,length(#)) arg(1)': ' @.s
end /*s*/
say pad 'element' right(s,widthH) arg(1)": " @.s

end /*s*/
say copies('─',40) /*show a pretty separator line. */
say copies('─',50) /*show a pretty separator line. */
return</lang>
return</lang>
'''output'''
'''output'''
Line 1,626: Line 1,619:
────────────────────────────────────────
────────────────────────────────────────
</pre>
</pre>

===version 2===
===version 2===
{{trans|PL/I}}
{{trans|PL/I}}