Counting sort
From Wikipedia, the free encyclopedia
Class | Sorting algorithm |
---|---|
Data structure | Array |
Worst case performance | O(n + k) |
Best case performance | O(n + k) |
Average case performance | O(n + k) |
Counting sort (sometimes referred to as ultra sort or math sort[1]) is a sorting algorithm which (like bucket sort) takes advantage of knowing the range of the numbers in the array to be sorted (array A). It uses this range to create an array C of this length. Each index i in array C is then used to count how many elements in A have the value i. The counts stored in C can then be used to put the elements in A into their right position in the resulting sorted array. The algorithm was created by Harold H. Seward in 1954.
Contents |
[edit] Characteristics of counting sort
Counting sort is a stable sort and has a running time of Θ(n+k), where n and k are the lengths of the arrays A (the input array) and C (the counting array), respectively. In order for this algorithm to be efficient, k must not be much larger than n.
The indices of C must run from the minimum to the maximum value in A to be able to index C directly with the values of A. Otherwise, the values of A will need to be translated (shifted), so that the minimum value of A matches the smallest index of C. (Translation by subtracting the minimum value of A from each element to get an index into C therefore gives a counting sort. If a more complex function is used to relate values in A to indices into C, it is a bucket sort.) If the minimum and maximum values of A are not known, an initial pass of the data will be necessary to find these (this pass will take time Θ(n); see selection algorithm).
The length of the counting array C must be at least equal to the range of the numbers to be sorted (that is, the maximum value minus the minimum value plus 1). This makes counting sort impractical for large ranges in terms of time and memory needed. Counting sort may for example be the best algorithm for sorting numbers whose range is between 0 and 100, but it is probably unsuitable for sorting a list of names alphabetically. However counting sort can be used in radix sort to sort a list of numbers whose range is too large for counting sort to be suitable alone.
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 sorting is inapplicable.
[edit] Tally sort
This article may contain inappropriate or misinterpreted citations that do not verify the text. Please help improve this article by checking for inaccuracies. (help, talk, get involved!) (September 2008) |
A well-known variant of counting sort is tally sort, where the input is known to contain no duplicate elements, or where we wish to eliminate duplicates during sorting. In this case the count array can be represented as a bit array; a bit is set if that key value was observed in the input array. Tally sort is widely familiar because of its use in the book Programming Pearls as an example of an unconventional solution to a particular set of limitations.[2]
[edit] The algorithm
A summary of the algorithm is as follows.
- Find the highest and lowest elements of the set
- Count the different elements in the array. (E.g. Set[4,4,4,1,1] would give three 4's and two 1's)
- Accumulate the counts. (E.g. Starting from the first element in the new set of counts, add the current element to the previous.)
- Fill the destination array from backwards: put each element to its countth position.
Each time you put in a new element decrease its count.
[edit] C/C++ implementation
void counting_sort(int *a, int n) { int i, min, max; min = max = a[0]; for(i = 1; i < n; i++) { if (a[i] < min) min = a[i]; else if (a[i] > max) max = a[i]; } int k = max - min + 1; int *c = (int *) malloc(k * sizeof(*a)); for(i = 0; i < k; i++) c[i] = 0; for(i = 0; i < n; i++) c[ a[i] - min ]++; int z, j = 0; for(i = min; i <= max; i++) for(z = 0; z < c[ i - min ]; z++) a[j++] = i; free(c); }
[edit] Notes
- ^ Anthony Ralston, Edwin D. Reilly, David Hemmendinger, ed (2003). "Ultrasort". Encyclopedia of Computer Science (4th ed.). Wiley. pp. 1660-1661. ISBN 0-470-86412-5.
- ^ Chapter 1 of Jon Bentley's Programming Pearls ISBN 0-201-10331-1.
[edit] References
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 8.2: Counting sort, pp.168–170.
- Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Second Edition. Addison-Wesley, 1998. ISBN 0-201-89685-0. Section 5.2, Sorting by counting, pp.75–80.
- Seward, Harold H. Information sorting in the application of electronic digital computers to business operations Masters thesis. MIT 1954.
- NIST's Dictionary of Algorithms and Data Structures: counting sort
[edit] External links
The Wikibook Algorithm implementation has a page on the topic of |
- Analyze Counting Sort in an online Javascript IDE
- Demonstration applet from Cardiff University
- Counting sort
- Counting Sort Simulation Java Applet
- Efficient Counting Sort in Haskell
|