This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Cs445 — Homework #2 Lower bound on Sorting, Radix Sort, SkipList and Random Variables Due: 3/1/2005 during class meeting. 1. Suggest an algorithm that sort n integers, each with k decimal digits in time O ( nk ). The algorithm uses k passes of counting sort, but does it from the most significant digit to the least significant digit. Discuss possible advantages and disadvantages of this algorithms comparing to the “traditional” radix sort. Answer: In radix sort based on most significant digit (MSD), the records are first sorted according to MSD and then each pile of records with the same LSD are sorted according to LSD seperately. Finally all the piles are combined together,in proper order. Example : MSD Sort : A1 B2 C3 A2 B1 A3 C1 B3 C2 Three Piles : A1 A2 A3; B2 B1 B3; C3 C1 C2 Each Pile is sorted seperately : A1 A2 A3; B1 B2 B3; C1 C2 C3. The psuedo code is : RadixSort(A,d) for i = d to 1 do Use a stable sort ( counting sort ) to sort array on digit i Pile each record with same most significant digit ( MSD ) i For each Pile call RadixSort(Pile, i 1); end for Combine all piles together in proper order. Radix Sort on Most Significant Digit: Advantage  Only as many digits are examined as are needed. Disadvantage  Memory intensive and requires more bookkeeping. The list must be repeatedly split into dublists for each value of the last digit processed. Radix Sort on Least Significant Digit: 1 Advantage  It is easier to work backwards from LSD since order based on MSD is not disturbed by LSD. Disadvantage  Requires padding of short keys if key length is variable.Also, all digits are examined even if the first 34 digits may contain all the information need to achieve a sorted order. 2. Assume that the running time of merge sort is O ( n log n ), and the running time of sorting n numbers, each of k decimals digits, using Radix sort, is O ( nk ). Prove that if no two numbers in the input are the same, then radix sort is always not faster than merge sort. Does this implies that you would always use merge sort over radix sort ? Answer: Since we are given that no two input numbers are the same, the number of digits in the largest number must be at least log 10 n . This is because, a ‘k’ digit decimal number, can have 10 k possible values. Thus, the fact that no two input numbers are the same implies that k ≥ log 10 n . There fore, it follows that radix sort is always not faster than merge sort....
View
Full
Document
This note was uploaded on 05/27/2008 for the course CS 445 taught by Professor Williams during the Spring '06 term at Arizona.
 Spring '06
 Williams
 Sort

Click to edit the document details