{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cse220sol6

# Solution we use an extra array c[1 k which provides

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the 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[i-1]; 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

{[ snackBarMessage ]}

### Page1 / 3

Solution We use an extra array C[1 k which provides...

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

View Full Document
Ask a homework question - tutors are online