This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Solution: We use an extra array C [1 : : : k ] which provides temporary working storage. The array is initialized to zero for each element. Here is the preprocessing procedure: for j=1 to n C[A[j]]=C[A[j]]++; for i=2 to k C[i]= C[i] + C[i1]; 1 After the rst loop, C [ i ] contains the number of elements equal to i . After the second loop, C [ i ] contains the number of elements less than or equal to i . The number of integers in the range ( a; b ] is computed as C [ b b c ] C [ b a c ]. The construction of the array C takes O ( n + k ) time and requires O ( k ) additional storage. Finally, the subtraction operation costs O (1) time, so the queries are answered in constant time. Problem 3: 8 pts You have an array of n data records. Each record has a key 0 or 1 : Design a O ( n ) algorithm to sort the data records according to the key values (those with key 0 should come before those with key 1). You can only use constant amount of additional storage during the sorting. Can you use your solution in radix sort so as to sort n records with b bit keys in O( bn ) time? (every key has b bits). Justify your answer....
View
Full Document
 Spring '09
 Algorithms, Data Structures, Sort, Sorting, The Key, verge

Click to edit the document details