Sorting algorithms/Counting sort: Difference between revisions

Content added Content deleted
m (Improved D entry)
m (→‎{{header|REXX}}: corrected a misspelling. -- ~~~~)
Line 1,242: Line 1,242:


=={{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 array elements. */
call show@ 'before sort' /*show before array elements*/
call show@ 'before sort' /*show before array elements*/
Line 1,250: Line 1,248:
call show@ ' after sort' /*show after array elements*/
call show@ ' after sort' /*show after array elements*/
exit
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

_.=0
_.=0
do j=1 for n

do j=1 for n
k=@.j
k=@.j
_.k=_.k+1
_.k=_.k+1
end
end

#=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


return
return




/*─────────────────────────────────────GEN@ subroutine─────────────*/
/*─────────────────────────────────────GEN@ subroutine─────────────*/
gen@: @.='' /*assign default value. */
gen@: @.='' /*assign default value. */

/* get the first 40 Recaman numbers: subtract if you can, */
/* get the first 40 Recaman numbers: subtract if you can, */
/* otherwise add. Another way: Recaman(1) = 1, */
/* otherwise add. Another way: Recaman(1) = 1, */
/* Recaman(n) = Recaman(n-1)-n if positive and new, else */
/* Recaman(n) = Recaman(n-1)-n if positive and new, else */
/* Recaman(n) = Recaman(n-1)+n */
/* Recaman(n) = Recaman(n-1)+n */

@.1 = 1
@.1 = 1
@.2 = 3
@.2 = 3
Line 1,336: Line 1,321:
highItem=highItem-1 /*adjust highItem slightly. */
highItem=highItem-1 /*adjust highItem slightly. */
return
return


/*─────────────────────────────────────SHOW@ subroutine────────────*/
/*─────────────────────────────────────SHOW@ subroutine────────────*/
show@: widthH=length(highItem) /*maximum width of any line.*/
show@: widthH=length(highItem) /*maximum width of any line.*/
Line 1,345: Line 1,328:
end
end


say copies('─',80) /*show a seperator line. */
say copies('─',80) /*show a separator line. */
return
return</lang>
'''output'''
</lang>
Output:
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">
element 1 before sort: 1
element 1 before sort: 1