Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Counting sort
(section)
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"/>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)