Sorting algorithms/Counting sort: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
(→{{header|REXX}}: arranged stemmed array assignments into columns for less space, indented DO loops more, added DO-END label comments. -- ~~~~) |
||
Line 1,249: | Line 1,249: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program sorts an array using the count-sort method. */ |
<lang rexx>/*REXX program sorts an array using the count-sort method. */ |
||
call gen@ /*generate array elements. */ |
call gen@ /*generate the array elements. */ |
||
call show@ 'before sort' /*show before array elements*/ |
call show@ 'before sort' /*show the before array elements.*/ |
||
call countSort highItem /* |
call countSort highItem /*ensure order in the universe.*/ |
||
call show@ ' after sort' /*show after array elements*/ |
call show@ ' after sort' /*show the after array elements.*/ |
||
exit /*stick a fork in it, we're done.*/ |
|||
exit |
|||
/*──────────────────────────────────countSORT subroutine────────────────*/ |
|||
/*─────────────────────────────────────countSORT subroutine────────*/ |
|||
countSort: procedure expose @.; parse arg n |
countSort: procedure expose @.; parse arg n; h=@.1 |
||
h=@.1 |
|||
L=h |
L=h |
||
do m=2 to n |
do m=2 to n |
||
L=min(L,@.m) |
L=min(L,@.m) |
||
h=max(h,@.m) |
h=max(h,@.m) |
||
end |
end /*m*/ |
||
_.=0 |
_.=0 |
||
do j=1 for n |
do j=1 for n |
||
k=@.j |
k=@.j |
||
_.k=_.k+1 |
_.k=_.k+1 |
||
end |
end /*j*/ |
||
#=1 |
#=1 |
||
do j=L to h |
do j=L to h |
||
if _.j\==0 then do #=# for _.j |
if _.j\==0 then do #=# for _.j |
||
@.#=j |
@.#=j |
||
end |
end |
||
end |
end /*j*/ |
||
return |
return |
||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/ |
|||
/*─────────────────────────────────────GEN@ subroutine─────────────*/ |
|||
gen@: @.= |
gen@: @.= /*assign default value for array.*/ |
||
/* assignt the first 40 Recaman numbers: subtract if you can, */ |
|||
/* otherwise add. Another way: Recaman(1) = 1, */ |
|||
/* Recaman(n) = Recaman(n-1)-n if positive and new, else */ |
|||
/* Recaman(n) = Recaman(n-1)+n */ |
|||
@.1 = 1 |
|||
@.2 = 3 |
|||
@.3 = 6 |
|||
@.4 = 2 |
|||
@.5 = 7 |
|||
@.6 = 13 |
|||
@.7 = 20 |
|||
@.8 = 12 |
|||
@.9 = 21 |
|||
@.10= 11 |
|||
@.11= 22 |
|||
@.12= 10 |
|||
@.13= 23 |
|||
@.14= 9 |
|||
@.15= 24 |
|||
@.16= 8 |
|||
@.17= 25 |
|||
@.18= 43 |
|||
@.19= 62 |
|||
@.20= 42 |
|||
@.21= 63 |
|||
@.22= 41 |
|||
@.23= 18 |
|||
@.24= 42 |
|||
@.25= 17 |
|||
@.26= 43 |
|||
@.27= 16 |
|||
@.28= 44 |
|||
@.29= 15 |
|||
@.30= 45 |
|||
@.31= 14 |
|||
@.32= 46 |
|||
@.33= 79 |
|||
@.34= 113 |
|||
@.35= 78 |
|||
@.36= 114 |
|||
@.37= 77 |
|||
@.38= 39 |
|||
@.39= 78 |
|||
@.40= 38 |
|||
@.1 = 1 ; @.11= 22 ; @.21= 63 ; @.31= 14 |
|||
⚫ | |||
@.2 = 3 ; @.12= 10 ; @.22= 41 ; @.32= 46 |
|||
@.3 = 6 ; @.13= 23 ; @.23= 18 ; @.33= 79 |
|||
@.4 = 2 ; @.14= 9 ; @.24= 42 ; @.34= 113 |
|||
@.5 = 7 ; @.15= 24 ; @.25= 17 ; @.35= 78 |
|||
@.6 = 13 ; @.16= 8 ; @.26= 43 ; @.36= 114 |
|||
@.7 = 20 ; @.17= 25 ; @.27= 16 ; @.37= 77 |
|||
@.8 = 12 ; @.18= 43 ; @.28= 44 ; @.38= 39 |
|||
@.9 = 21 ; @.19= 62 ; @.29= 15 ; @.39= 78 |
|||
@.10= 11 ; @.20= 42 ; @.30= 45 ; @.40= 38 |
|||
⚫ | |||
end |
end |
||
highItem=highItem-1 /*adjust highItem slightly. */ |
highItem=highItem-1 /*adjust highItem slightly. */ |
||
return |
return |
||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/ |
|||
/*─────────────────────────────────────SHOW@ subroutine────────────*/ |
|||
show@: widthH=length(highItem) /* |
show@: widthH=length(highItem) /*max width of any output line. */ |
||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
return</lang> |
return</lang> |
||
'''output''' |
'''output''' |