In this post we are discussing about Write an algorithm to find max element in the array of n elements Give mathematical analysis of this algorithm
To find maximum element in the given array:-
Algorithm
MaxElement(A[0..n — 1]){
/[Determines the value of the largest element in a given al
//input: An array — II of real numbers
//Output: The value of the largest element in A
maxval ← A[0]
for i ← 1 to n — 1 do
if A[i] > maxval
maxval ← A[i]
return maxval
Here comparison is the basic operation.
Note:- that number of comparisons will be same for all arrays of size n. Therefore, no need to distinguish worst, best and average cases.
Total number of basic operations (comparison) are,