Mark As Completed Discussion

Linear Search Method

In the Linear Search method, we'll implement the following steps.

  • Initialize the minimum and maximum values to the first element of the array.
  • Traverse the array except for the first element.
  • Update the minimum and maximum values after comparing them with the values in the array on each iteration accordingly.

Let's have a look at the Pseudocode first:

Pseudocode

SNIPPET
1int[] findMinimumAndMaximum(int input[], int size)
2{
3    int maximum = input[0]
4    int minimum = input[0]
5    for ( i = 1 to size-1 )
6    {
7        if ( input[i] > maximum )
8            maximum = input[i]
9        else if ( input[i] < minimum )
10            minimum = input[i]
11    }
12    int result[2] = {maximum, minimum}
13    return result
14}

Now, we'll implement the above steps in some other languages:

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment