Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Counting sort
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Variant algorithms== If each item to be sorted is itself an integer, and used as key as well, then the second and third loops of counting sort can be combined; in the second loop, instead of computing the position where items with key <code>i</code> should be placed in the output, simply append <code>Count[i]</code> copies of the number <code>i</code> to the output. This algorithm may also be used to eliminate duplicate keys, by replacing the <code>Count</code> array with a [[bit vector]] that stores a <code>one</code> for a key that is present in the input and a <code>zero</code> for a key that is not present. If additionally the items are the integer keys themselves, both second and third loops can be omitted entirely and the bit vector will itself serve as output, representing the values as offsets of the non-<code>zero</code> entries, added to the range's lowest value. Thus the keys are sorted and the duplicates are eliminated in this variant just by being placed into the bit array. For data in which the maximum key size is significantly smaller than the number of data items, counting sort may be [[parallel algorithm|parallelized]] by splitting the input into subarrays of approximately equal size, processing each subarray in parallel to generate a separate count array for each subarray, and then merging the count arrays. When used as part of a parallel radix sort algorithm, the key size (base of the radix representation) should be chosen to match the size of the split subarrays.<ref>{{citation | last1 = Zagha | first1 = Marco | last2 = Blelloch | first2 = Guy E. | author2-link = Guy Blelloch | contribution = Radix sort for vector multiprocessors | doi = 10.1145/125826.126164 | pages = 712β721 | publisher = IEEE Computer Society / ACM | title = Proceedings of Supercomputing '91, November 18-22, 1991, Albuquerque, NM, USA | year = 1991| isbn = 0897914597 | url = https://www.cs.cmu.edu/~scandal/papers/cray-sort-supercomputing91.ps.gz| doi-access = free }}.</ref> The simplicity of the counting sort algorithm and its use of the easily parallelizable prefix sum primitive also make it usable in more fine-grained parallel algorithms.<ref>{{citation | last = Reif | first = John H. | author-link = John Reif | contribution = An optimal parallel algorithm for integer sorting | doi = 10.1109/SFCS.1985.9 | pages = 496β504 | title = [[Symposium on Foundations of Computer Science|Proc. 26th Annual Symposium on Foundations of Computer Science (FOCS 1985)]] | year = 1985| isbn = 0-8186-0644-4 | s2cid = 5694693 }}.</ref> As described, counting sort is not an [[in-place algorithm]]; even disregarding the count array, it needs separate input and output arrays. It is possible to modify the algorithm so that it places the items into sorted order within the same array that was given to it as the input, using only the count array as auxiliary storage; however, the modified in-place version of counting sort is not stable.<ref name="sedgewick"/>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Counting sort
(section)
Add topic