Counting sort is an efficient algorithm for sorting an array of elements that each have a nonnegative integer key, for example, an array, sometimes called a list, of positive integers could have keys that are just the value of the integer as the key, or a list of words could have keys assigned to them by some scheme mapping the alphabet to integers (to sort in alphabetical order, for instance). Unlike other sorting algorithms, such as mergesort, counting sort is an integer sorting algorithm, not a comparison based algorithm. While any comparison based sorting algorithm requires comparisons, counting sort has a running time of when the length of the input list is not much smaller than the largest key value, , in the list. Counting sort can be used as a subroutine for other, more powerful, sorting algorithms such as radix sort.
An algorithm that maps the following input/output pair is called a sorting algorithm:
Input: An array, , of size of orderable elements.
Output: A sorted permutation of , called , such that
Here is what it means for an array to be sorted.
An array is sorted if and only if for all ,
By convention, empty arrays and singleton arrays (arrays consisting of only one element) are always sorted. This is a key point for the base case of many sorting algorithms.
Counting sort assumes that each of the input elements in a list has a key value ranging from to , for some integer . For each element in the list, counting sort determines the number of elements that are less than it. Counting sort can use this information to place the element directly into the correct slot of the output array.
Counting sort uses three lists: the input list, , the output list, , and a list that serves as temporary memory, . Note that and have slots (a slot for each element), while contains slots (a slot for each key value).
Counting sort starts by going through , and for each element , it goes to the index of that has the same value as (so it goes to ) and increments the value of by one. This means that if has seven ’s in its list, after counting sort has gone through all elements of , the value at will be . Similarly, if has two ’s, after counting sort has gone through all of the elements of , (using 0 indexing) will be equal to . In this step, keeps track of how many elements in there are that have the same value of a particular index in . In other words, the indices of correspond to the values of elements in , and the values in correspond to the total number of times that a value in appears in .
Next, modify so that each includes the number of elements less than it. This can be accomplished by going through and replacing each value with . This step allows counting sort to determine at what index in an element should be placed.
Then, starting at the end of , add elements to by checking the value of , going to , writing the value of the element at to . Finally, decrement the value of by since that slot in is now occupied.
This animation illustrates counting sort:
Here is one way to implement counting sort in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Counting sort has a running time.
The first loop goes through , which has elements. This step has a running time. The second loop iterates over , so this step has a running time of . The third loop iterates through , so again, this has a running time of . Therefore, the counting sort algorithm has a running time of .
Counting sort is efficient if the range of input data, , is not significantly greater than the number of objects to be sorted, .
Counting sort is a stable sort with a space complexity of .