Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3740695 • Letter: 1

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 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).

Explanation / Answer

outer loop

for i in range(n) -> n times

inner loop while(bits[j])= 1

runs p times at max

So it takes -> O (P)

So whole program runs NP times

p = floor(log2(n))

max value of p = log2(n)

Big - O = O ( N Log2 (N))

min value of p = 1

? ( N 1) = ?(N)

p = floor(log2(n))

max value of p = log2(n)

Big - O = O ( N Log2 (N))

min value of p = 1

? ( N 1) = ?(N)