Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting in linear time Idea: if we can assume there are only k possible values to sort, we have extra information about where each element might need.

Similar presentations


Presentation on theme: "Sorting in linear time Idea: if we can assume there are only k possible values to sort, we have extra information about where each element might need."— Presentation transcript:

1 Sorting in linear time Idea: if we can assume there are only k possible values to sort, we have extra information about where each element might need to go in the final solution How does this help? We can now execute the following idea for sorting more efficiently: If we see an element x, and can determine that m elements of the array are <= x, we know x needs to be in position m + 1

2 Counting sort Let A be the given array of length n, B be the array to hold our final answer Create an array C of length k and hold all 0’s Increment each C[A[i]] for each i = 1 to n Calculate C[i] = C[i] + C[i-1] for each i = 1 to k C[i] will hold the number of items less than or equal to i For j = n to 1 B[C[A[j]]] = A[j] // Copy A[j] into the last place of the A[j] “block” C[A[j]] = C[A[j]] – 1 // Decrement the number of items below A[j]

3 Counting sort example A = {5, 3, 2, 5, 3, 1, 6}, k = 9
C after second for-loop: {0, 1, 2, 4, 4 ,6, 7, 7, 7, 7} C after assignment into B: {0, 0, 1, 2, 4, 4, 6, 7, 7, 7} B after final loop: {1, 2, 3, 3, 5, 5, 6}

4 Analysis of Counting Sort
Counting sort is stable – even if there are multiple copies of a number, they will appear in the same order in the output as in the input Important when there is “satellite data” – i.e. we are sorting a more complicated data structure Running time: by inspection the time required is 𝑂(𝑛+𝑘) so if 𝑘=𝑂(𝑛) the total time is just 𝑂(𝑛) and beats Mergesort!

5 Radix Sort Need: access to a stable sorting algorithm to be called as a subroutine Algorithm RadixSort(int arr[n]) For i = 1 to largest number of digits in any of the numbers Sort the entries according to the i’th digit from the right

6 Radix Sort Analysis If the numbers have at most d digits and are represented in base-k, the time is 𝑂 𝑑 𝑛+𝑘 if we use counting sort as our subroutine Counting sort takes time n + k Need to sort d times Allowing d digits in base k means entries can have value up to 𝑘 𝑑 −1


Download ppt "Sorting in linear time Idea: if we can assume there are only k possible values to sort, we have extra information about where each element might need."

Similar presentations


Ads by Google