Sorting algorithms/Counting sort: Difference between revisions

Content added Content deleted
(+ AutoHotkey)
(formatting)
Line 5: Line 5:


Pseudocode:
Pseudocode:
'''function''' ''countingSort''(array, min, max):

countingSort(''array'', ''min'', ''max''):
count: '''array of''' (max - min + 1) '''elements'''
''count'': '''array''' of (''max'' - ''min'' + 1) elements
'''initialize''' count '''with''' 0
'''for each''' number '''in''' array '''do'''
initialize ''count'' with 0s
count[number - min] := count[number - min] + 1
'''foreach''' ''number'' '''in''' ''array'':
'''done'''
''count''[''number'' - ''min''] := ''count''[''number'' - ''min''] + 1
z := 0
'''end''' '''foreach'''
'''for''' i '''from''' min '''to''' max '''do'''
''z'' := 0
'''for''' ''i'' '''from''' ''min'' '''to''' ''max'':
'''while''' ( count[i - min] > 0 ) '''do'''
array[z] := i
'''while''' ( ''count''[''i'' - ''min''] > 0 )
''array''[''z''] := ''i''
z := z+1
''z'' := ''z'' + 1
count[i - min] := count[i - min] - 1
''count''[''i'' - ''min''] := ''count''[''i'' - ''min''] - 1
'''done'''
'''end''' '''while'''
'''done'''
'''end''' '''for'''
'''end''' countingSort


The ''min'' and ''max'' can be computed apart, or be known ''a priori''.
The ''min'' and ''max'' can be computed apart, or be known ''a priori''.


'''Note''': we know that, given an array of integers, its maximum and minimum values can be always found; but if we imagine the worst case for an array of 32 bit integers, we see that in order to hold the counts, we need an array of 2<sup>32</sup> elements, i.e. we need, to hold a count value up to 2<sup>32</sup>-1, more or less 16 Gbytes. So the counting sort is more practical when the range is (very) limited and minimum and maximum values are known ''a priori''. (Anyway sparse arrays may limit the impact of the memory usage)
'''Note''': we know that, given an array of integers, its maximum and minimum values can be always found; but if we imagine the worst case for an array of 32 bit integers, we see that in order to hold the counts, we need an array of 2<sup>32</sup> elements, i.e., we need, to hold a count value up to 2<sup>32</sup>-1, more or less 16 Gbytes. So the counting sort is more practical when the range is (very) limited and minimum and maximum values are known ''a priori''. (Anyway sparse arrays may limit the impact of the memory usage)


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 79: Line 77:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276465.html#276465 forum]
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276465.html#276465 forum]
<lang AutoHotkey>
<lang AutoHotkey>MsgBox % CountingSort("-1,1,1,0,-1",-1,1)
MsgBox % CountingSort("-1,1,1,0,-1",-1,1)


CountingSort(ints,min,max) {
CountingSort(ints,min,max) {
Line 333: Line 330:
endwhile
endwhile
endfor
endfor
endfunction
endfunction</lang>
</lang>


Testing:
Testing:
Line 451: Line 447:
>>> mini,maxi = 1,10
>>> mini,maxi = 1,10
>>> countingSort(data, mini, maxi) == sorted(data)
>>> countingSort(data, mini, maxi) == sorted(data)
True
True</lang>
</lang>


Using a list:
Using a list: