Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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
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