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 |
<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' |
call show@ 'before sort' /*show the before array elements.*/ |
||
call countSort |
call countSort # /*sort N entries of the @. array.*/ |
||
call show@ ' after sort' |
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: procedure expose @.; |
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; |
_.=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 |
return |
||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/ |
/*──────────────────────────────────GEN@ subroutine─────────────────────*/ |
||
gen@: |
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 |
|||
do j=1 for # /* [↓] assign 40 Recaman numbers.*/ |
|||
@.j=word($,j) /*assign a Recaman # from a list.*/ |
|||
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*/ |
|||
say left('',9) "element" right(s,length(#)) arg(1)': ' @.s |
|||
⚫ | |||
say pad 'element' right(s,widthH) arg(1)": " @.s |
|||
⚫ | |||
say copies('─', |
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}} |