Algorithm to find Maximum and Minimum number in an Array

For Complete YouTube Video: Click Here

In this class, we will try to understand the algorithm to find Maximum and Minimum number in an Array.

We have already discussed the concepts of sorting and searching algorithms in our previous classes.

Algorithm to find Maximum and Minimum number in an Array

We will consider the following array of elements to understand the above algorithm clearly.

In the above array, with our intuition, we can say that the maximum element is nine and the minimum element is 1.

Before understanding the algorithm, we will understand the working mechanism to find an array’s maximum and minimum elements.

First, we will consider the element in the zeroth index as the maximum and minimum element.

In our case, the maximum and minimum element is 3.

With that assumption, we will proceed.

We will start with the element in the first index and compare it with the existing maximum and minimum elements.

We will compare seven with three existing maximum and minimum elements in our case.

Seven is greater than three, so the new maximum element is seven.

Similarly, seven is NOT smaller than three, so three will be our minimum element.

In the same way, we will compare all the elements in the array until the end.

In the end, we will get the maximum and minimum elements in the array.

The image is an algorithm to find maximum and minimum number in an array.

To find the maximum and minimum element, the loop in the algorithm will iterate from the first index till the end of the array.

In every iteration, we will compare each array element with the existing maximum and minimum elements.

By the end of all iterations, we will get the maximum and minimum elements of the array.