rec11 - 6.006 Intro to Algorithms Recitation 11 March 11,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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, theres 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 youll 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

Page1 / 6

rec11 - 6.006 Intro to Algorithms Recitation 11 March 11,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online