Counting Sort

In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently.

Because counting sort uses key values as indexes into an array, it is not a comparison sort, and the Ω(n log n) lower bound for comparison sorting does not apply to it. Bucket sort may be used for many of the same tasks as counting sort, with a similar time analysis; however, compared to counting sort, bucket sort requires linked lists, dynamic arrays or a large amount of preallocated memory to hold the sets of items within each bucket, whereas counting sort instead stores a single number (the count of items) per bucket.

Read more about Counting Sort:  Input and Output Assumptions, The Algorithm, Analysis, Variant Algorithms, History

Famous quotes containing the words counting and/or sort:

    Is it not manifest that our academic institutions should have a wider scope; that they should not be timid and keep the ruts of the last generation, but that wise men thinking for themselves and heartily seeking the good of mankind, and counting the cost of innovation, should dare to arouse the young to a just and heroic life; that the moral nature should be addressed in the school-room, and children should be treated as the high-born candidates of truth and virtue?
    Ralph Waldo Emerson (1803–1882)

    There is a sort of homely truth and naturalness in some books which is very rare to find, and yet looks cheap enough. There may be nothing lofty in the sentiment, or fine in the expression, but it is careless country talk. Homeliness is almost as great a merit in a book as in a house, if the reader would abide there. It is next to beauty, and a very high art. Some have this merit only.
    Henry David Thoreau (1817–1862)