Question: 1. [4 marks] Binary representation and algorithm analysis. Consider the following algorithm, which manually counts up to a given number n, using an array of

 1. [4 marks] Binary representation and algorithm analysis. Consider the following

1. [4 marks] Binary representation and algorithm analysis. Consider the following algorithm, which manually counts up to a given number n, using an array of O's and 1's to mimic binary notation. 1 from math import floor, log2 3 4 def count (n: int) -None: # Precondition: n> 0. p- floor (log2(n)) + 1 bits = [0] * p # The number of bits required to represent n. # Initialize an array of length p with all 0's. 6 7 for i in range (n): #1-0, 1, n-1 , # Increment the current count in the bits array. This adds 1 to # the current number, basically using the loop to act as a "carry" operation. j=p-1 while bits[j]1: 10 12 13 14 15 16 bits bits[j] 1 For this question, assume each individual line of code in the above algorithm takes constant time, ie. counts as a single step. (This includes the [O] p line.) (a) 3 marks] Prove that the running time of this algorithm is O(n log n). (b) [1 mark] Prove that the running time of this algorithm is ?(n)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!