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

Please Show the work to how the upper bound was found For each of the following

ID: 3755751 • Letter: P

Question

Please Show the work to how the upper bound was found

For each of the following code fragments, your task is to estimate its running time, in terms of n. You should assume that the value of n is defined elsewhere in the program. The running time that you report should be the tightest upper bound you can find. Don't just say everything is O(2t), because although correct, it is not precise enough. Upper bounds that are too big will receive deductions during grading. For each code sample: 1. State an upper bound on the running time of the program fragment using O() notation. 2. Briefly justify your upper bound.

Explanation / Answer

Hey,

Below are the answers to your question

Fragment a

let's say it will run k times

So at kth time t will become at last 1

At any ith time t is n/(2^i)

So, at i=k n/(2^k)=1

Therfore k=log2(n)

So, the upper bound will be O(logn)

Fragment 2

Since in this fragment one loop to fragment 1 is added. So, for the loop time is O(n) and inside its O(logn)

So, total time would be O(log1)+O(log2).....O(logn)=O(log(n!))=O(nlogn).

Fragment 3

In this outer loop will runO(log(n)) times and inner loop at any t will run O(t) times.

So, totally time=n+n/2+n/(2^2).....+n/(2^logk) where k=logn

It is a GP

So, time=n(1*(1-(1/2)^k)/0.5)=O(n^2)

Fragment 4

This fragment will not work because initially t=n+5 which is greater than n.

So time upper bound is O(1)

Fragment 5

It is basically a loop with n terms. So, time upper bound is O(n).

Kindly revert for any queries

Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote