Discrete Math or Computer science math FYI This is only one

Discrete Math (or Computer science math) (FYI: This is only one question)(Please Answer if you know answer the both parts)

Write an algorithm to find the largest and smallest of n values and what is the best (least upper bound) theta notation and why?!

Thanks

Solution

Algorithm to find smallest and largest of n valsurs

Algorithm (list A)

{

A : list

N : length of A

//let assume smallest and largest element is first element

Small = A[0]

Large = A[0]

for i=0 to N

{

if Small > A[i] then Small = A[i]

if Large < A[i] then Large = A[i]

}

//after completion of above loop variable Small contain smallest number and Large contain largest number..

End

}

Time complexity for above problem is O(n).. Because it contains one loop

Discrete Math (or Computer science math) (FYI: This is only one question)(Please Answer if you know answer the both parts) Write an algorithm to find the larges

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site