Consider the following code segment 1 Total 0 2 for 0 xSolut

Consider the following code segment: (1) Total 0; (2) for 0, x

Solution

1.Total =0

O(1): so setting a counter to zero is a constant time operation. Here we are setting a chunk of memory to a new value. Here we are hardcoded the value to zero which is a constant time operation. There is no way to call this function or alter the state that we could change the operation. This is O(1)

2.O(nlogn) :

0+log(1)+...+lon(n)

Simple addition of nNum with increment for n times

3O(nlonn^2)

Simple addition of nNum with multiplication of nNum with nNum for n times

4.total++ which is nothing but total=total+1

Here we are incrementing total value by 1 each time. Addition of 1 is equality which is also a constant time

O(1)+O(1);

5.So total complexity

O(1)+O(nlonn)+O(nlogn^2)+O(1)

2O(1)+O(nlogn)+O(2nlogn)

2O(1)+O(3nlogn)

 Consider the following code segment: (1) Total 0; (2) for 0, xSolution1.Total =0 O(1): so setting a counter to zero is a constant time operation. Here we are s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site