Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

How are standard costs used in the control process?

Answered: 1 week ago

Question

4. Describe cultural differences that influence perception

Answered: 1 week ago