Sorting algorithms/Counting sort: Difference between revisions

→‎{{header|REXX}}: arranged stemmed array assignments into columns for less space, indented DO loops more, added DO-END label comments. -- ~~~~
(→‎{{header|REXX}}: arranged stemmed array assignments into columns for less space, indented DO loops more, added DO-END label comments. -- ~~~~)
Line 1,249:
 
=={{header|REXX}}==
<lang rexx>/*REXX program sorts an array using the count-sort method. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call countSort highItem /*invokeensure the count sort.order in the universe.*/
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; h=@.1
h=@.1
L=h
do m=2 to n
L=min(L,@.m)
h=max(h,@.m)
end /*m*/
_.=0
do j=1 for n
k=@.j
_.k=_.k+1
end /*j*/
#=1
do j=L to h
if _.j\==0 then do #=# for _.j
@.#=j
end
end /*j*/
 
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
/*─────────────────────────────────────GEN@ subroutine─────────────*/
gen@: @.='' /*assign default value. for array.*/
/* getassignt 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
do highItem=1 while @.highItem\=='' /*find how many entries. */
@.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
 
do highItem=1 while @.highItem\=='' /*find out how many entries. exist*/
end
 
highItem=highItem-1 /*adjust highItem slightly. */
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
/*─────────────────────────────────────SHOW@ subroutine────────────*/
show@: widthH=length(highItem) /*maximummax width of any output line. */
do j=1 for highItem
 
say 'element' right(j,widthH) arg(1)":" @.j
do j=1 for highItem
end
say 'element' right(j,widthH) arg(1)":" @.j
say copies('─',80) /*show a pretty separator line. */
end
 
say copies('─',80) /*show a separator line. */
return</lang>
'''output'''