Analyze the time complexity of the algorithm below Use the B

Analyze the time complexity of the algorithm below.

Use the Big- notation: T(n)= (?). Provide justification for your result.

DoSomething(A,n)

1 for (i=n;i>=1;i--)

2 maxIndx=i

3 maxValue=A[i]

4 for (j=1;j<i;j++)

5 if (A[j]>A[maxIndx])

6 maxIndx=j

7 maxValue=A[j]

8 A[maxIndx]=A[i]

9 A[i]=maxValue

10 cnt5th=0

11 for (i=1;i<=n;i++)

12 if (A[i]==A[5])

13 cnt5th++

14 return cnt5th

Solution

The time Complexity is O(n)

The Reason Being

Statement 2 Executes n Times.

Statement 5 and 6 Executes n Times.

Statement 12 Executes n Times.

So total 3n times + constant c

i.e, 3n+c

But the Maximum value is time complexity hence O(3n)

but constants do not have significance hence O(n)

Analyze the time complexity of the algorithm below. Use the Big- notation: T(n)= (?). Provide justification for your result. DoSomething(A,n) 1 for (i=n;i>=1

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site