This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 6.006 Intro to Algorithms Recitation 11 March 11, 2011 Counting Sort Counting sort is an algorithm that takes an array A of n elements in the range { 1, 2, ..., k } and sorts the array in O ( n + k ) time. Counting sort uses no comparisons and uses the fact that the n elements are in a limited range to beat the O ( n log n ) limit of comparison sorts. Algorithm: Counting sort keeps an auxiliary array C with k elements, all initialized to 0. We make one pass through the input array A and for each element i in A that we see, we increment C [ i ] by 1. After we iterate through the n elements of A and update C , the value at index j of C corresponds to how many times j appeared in A . This step takes O ( n ) time to iterate through A . Once we have C , we can construct the sorted version of A by iterating through C and inserting each element j a total of C [ j ] times into a new list (or A itself). Iterating through C takes O ( k ) time. The end result is a sorted A and in total it took O ( n + k ) time to do so. Note that this does not permute the elements in A into a sorted list. If A had two 3s for example, there’s no distinction which 3 mapped to which 3 in the sorted result. We just counted two 3s and arbitrarily stuck two 3s in the sorted list. This is perfectly fine in many cases, but you’ll see later on in radix sort why in some cases it is preferable to be able to provide a permutation that transforms A into a sorted version of itself. To do this, we continue from the point where C is an array where C [ j ] refers to how many times j appears in A . We transform C to an array where C [ j ] refers to how many elements are ≤ j . We do this by iterating through C and adding the value at the previous index to the value at the current index, since the number of elements ≤ j is equal to the number of elements ≤ j 1 (i.e. the value at the previous index) plus the number of elements = j (i.e. the value at the current index). The final result is a matrix C where the value of C [ j ] is the number of elements ≤ j in A . Now we iterate through A backwards starting from the last element of A . For each element i we see, we check C [ i ] to find out how many elements are there ≤ i . From this information, we 6.006 Intro to Algorithms Recitation 11 March 11, 2011 know exactly where we can put i in the sorted array. Once we insert i into the sorted array, we decrement C [ i ] so that if we see a duplicate element, we know that we have to insert it right before the previous i . Once we finish iterating through A , we will get a sorted list as before. This time, we provided a mapping from each element A to the sorted list. Note that since we iterated through A backwards and decrement...
View
Full
Document
This note was uploaded on 11/11/2011 for the course MATH 180 taught by Professor Byrns during the Spring '11 term at Montgomery College.
 Spring '11
 byrns
 Math, Counting

Click to edit the document details