Sorting algorithms/Counting sort: Difference between revisions

Content added Content deleted
(Nimrod -> Nim)
Line 436: Line 436:
? arr
? arr
# value: [0, 0, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 34, 34, 34, 37, 56, 67, 76, 78, 234, 435, 435, 453, 467, 534, 634, 735].diverge()</pre>
# value: [0, 0, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 34, 34, 34, 37, 56, 67, 76, 78, 234, 435, 435, 453, 467, 534, 634, 735].diverge()</pre>


=={{header|Eiffel}}==

<lang Eiffel>
class
COUNTING_SORT
feature
sort(ar: ARRAY[INTEGER]; min, max: INTEGER): ARRAY[INTEGER]
local
count: ARRAY[INTEGER]
i, j, z: INTEGER
do
create count.make_filled (0, 0, max-min)
from
i:= 0
until
i= ar.count
loop
count[ar[i]-min]:= count[ar[i]-min]+1
i:= i+1
end
across count as c loop io.put_string (c.item.out + "%T") end
z:= 0
from i:= min
until i>max
loop
from j:= 0
until j= count[i-min]
loop
ar[z]:=i
z:= z+1
j:= j+1
end
i:= i+1
end
Result:= ar
end
end
</lang>
TEST:
<lang Eiffel>
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
create test.make_filled(0,0,5)
test[0]:=-7
test[1]:=4
test[2]:=2
test[3]:=6
test[4]:=1
test[5]:=3
across test as t loop io.put_string (t.item.out + "%T") end
create count
test:=count.sort (test, -7, 6)
across test as ar loop io.put_string (ar.item.out+"%T") end
end
count: COUNTING_SORT
test: ARRAY[INTEGER]
end
</lang>
<pre>
-7 4 2 6 1 3
-7 1 2 3 4 6


=={{header|Fortran}}==
=={{header|Fortran}}==