Each of the following ten questions has one of the following
Solution
a)
This will be O(n). Correct option is c.
n/5 will run with O(n) and 5/n will also take n. So overall it will take O(n)
b)
it will be O(n).Correct option is c.
it will run with resepct to n for each iteration so O(n)
c)
It is also O(n) as it will run till i reaches n.
Correct option is C.
d)
Here we have two notations one is T(n) other is logn. The next term depends upon previous term plus logn.
So overall running time will be logn.
e)
In this case overall running time will be O(n)
As the term depends on previous value of N plus O(n).
f)
It should also be O(n)
g)
it will be constant always so running time will be O(1)
h)
It will be D. O(nlogn) we can use quick sort.
i)
In unsorted array it will take O(n)
Correct option is C as we need to traverse the whole array to find this.
j)
Correct option is B O(logn).
In case of sorted array we can do binary search (divide and conquer) to find the element in the array.
