Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help! Thank you 4. Counting Sort (4 points) Al gorithm 1int counting Sort(int A[1...nl, int k) 1: //Precondition: The n values in A are
Please help! Thank you
4. Counting Sort (4 points) Al gorithm 1int counting Sort(int A[1...nl, int k) 1: //Precondition: The n values in A are all between 0 and k 2: Let ClOk be a new array 3: //We will store our sorted array in the array B 4: Let B[1...n] be a new array 5: for i- 0 to k do 7: end for 8: for i-1 to n do 10: end for 11: //Ci] now contains the # of elements in A equal to i 12: for i-1 to k do 14: end for 15: //Cli] now contains the # of elements in A that are-to i 16: for i - n down to 1 do 17: BCAA 19: end for 20: return B; (a) (2 points) Illustrate the operation of countingSort(12,1,5, 3, 1,2,5],6) Specifically, show the changes made to the arrays A, B, and C for each pass through the for loop at line 16. (b) (2 points) Describe an algorithm that, given an unsorted array of n integers in the range 0 to k, preprocesses its input and then answers any query about how many of the n integers fall into a range a to lb in O(1) time. Your algorithm should use O(n+k) preprocessing time. (Hint: Look at the array C which is computed by the above code for inspiration)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started