How to find maximum and minimum of an array using Linear search ?

0 0
Read Time:5 Minute, 45 Second

Process to create Sort :

Create values of min and max as minimum and maximum of the first two elements respectively. Starting from 3rd, compare each element with max and min, and change max and min accordingly (i.e., if the element is smaller than min then change min, else if the element is greater than max then change max, else ignore the element)

Below is the code created in multiple languages of the above approach:

// C++ program of above implementation
#include<iostream>
using namespace std;

// Pair struct is used to return
// two values from getMinMax()
struct Pair
{
	int min;
	int max;
};

Pair getMinMax(int arr[], int n)
{
	struct Pair minmax;	
	int i;
	
	// If there is only one element
	// then return it as min and max both
	if (n == 1)
	{
		minmax.max = arr[0];
		minmax.min = arr[0];	
		return minmax;
	}
	
	// If there are more than one elements,
	// then initialize min and max
	if (arr[0] > arr[1])
	{
		minmax.max = arr[0];
		minmax.min = arr[1];
	}
	else
	{
		minmax.max = arr[1];
		minmax.min = arr[0];
	}
	
	for(i = 2; i < n; i++)
	{
		if (arr[i] > minmax.max)	
			minmax.max = arr[i];
			
		else if (arr[i] < minmax.min)	
			minmax.min = arr[i];
	}
	return minmax;
}

// Driver code
int main()
{
	int arr[] = { 1000, 11, 445,
				1, 330, 3000 };
	int arr_size = 6;
	
	struct Pair minmax = getMinMax(arr, arr_size);
	
	cout << "Minimum element is "
		<< minmax.min << endl;
	cout << "Maximum element is "
		<< minmax.max;
		
	return 0;
}

/* structure is used to return two values from minMax() */
#include<stdio.h>
struct pair
{
int min;
int max;
};

struct pair getMinMax(int arr[], int n)
{
struct pair minmax;	
int i;

/*If there is only one element then return it as min and max both*/
if (n == 1)
{
	minmax.max = arr[0];
	minmax.min = arr[0];	
	return minmax;
}

/* If there are more than one elements, then initialize min
	and max*/
if (arr[0] > arr[1])
{
	minmax.max = arr[0];
	minmax.min = arr[1];
}
else
{
	minmax.max = arr[1];
	minmax.min = arr[0];
}

for (i = 2; i<n; i++)
{
	if (arr[i] > minmax.max)	
	minmax.max = arr[i];

	else if (arr[i] < minmax.min)	
	minmax.min = arr[i];
}

return minmax;
}

/* Driver program to test above function */
int main()
{
int arr[] = {1000, 11, 445, 1, 330, 3000};
int arr_size = 6;
struct pair minmax = getMinMax (arr, arr_size);
printf("nMinimum element is %d", minmax.min);
printf("nMaximum element is %d", minmax.max);
getchar();
}
// Java program of above implementation
public class GFG {
/* Class Pair is used to return two values from getMinMax() */
	static class Pair {

		int min;
		int max;
	}

	static Pair getMinMax(int arr[], int n) {
		Pair minmax = new Pair();
		int i;

		/*If there is only one element then return it as min and max both*/
		if (n == 1) {
			minmax.max = arr[0];
			minmax.min = arr[0];
			return minmax;
		}

		/* If there are more than one elements, then initialize min
	and max*/
		if (arr[0] > arr[1]) {
			minmax.max = arr[0];
			minmax.min = arr[1];
		} else {
			minmax.max = arr[1];
			minmax.min = arr[0];
		}

		for (i = 2; i < n; i++) {
			if (arr[i] > minmax.max) {
				minmax.max = arr[i];
			} else if (arr[i] < minmax.min) {
				minmax.min = arr[i];
			}
		}

		return minmax;
	}

	/* Driver program to test above function */
	public static void main(String args[]) {
		int arr[] = {1000, 11, 445, 1, 330, 3000};
		int arr_size = 6;
		Pair minmax = getMinMax(arr, arr_size);
		System.out.printf("\nMinimum element is %d", minmax.min);
		System.out.printf("\nMaximum element is %d", minmax.max);

	}

}
# Python program of above implementation

# structure is used to return two values from minMax()

class pair:
	def __init__(self):
		self.min = 0
		self.max = 0

def getMinMax(arr: list, n: int) -> pair:
	minmax = pair()

	# If there is only one element then return it as min and max both
	if n == 1:
		minmax.max = arr[0]
		minmax.min = arr[0]
		return minmax

	# If there are more than one elements, then initialize min
	# and max
	if arr[0] > arr[1]:
		minmax.max = arr[0]
		minmax.min = arr[1]
	else:
		minmax.max = arr[1]
		minmax.min = arr[0]

	for i in range(2, n):
		if arr[i] > minmax.max:
			minmax.max = arr[i]
		elif arr[i] < minmax.min:
			minmax.min = arr[i]

	return minmax

# Driver Code
if __name__ == "__main__":
	arr = [1000, 11, 445, 1, 330, 3000]
	arr_size = 6
	minmax = getMinMax(arr, arr_size)
	print("Minimum element is", minmax.min)
	print("Maximum element is", minmax.max)

// C# program of above implementation
using System;

class GFG
{
	/* Class Pair is used to return
	two values from getMinMax() */
	class Pair
	{
		public int min;
		public int max;
	}

	static Pair getMinMax(int []arr, int n)
	{
		Pair minmax = new Pair();
		int i;

		/* If there is only one element
		then return it as min and max both*/
		if (n == 1)
		{
			minmax.max = arr[0];
			minmax.min = arr[0];
			return minmax;
		}

		/* If there are more than one elements,
		then initialize min and max*/
		if (arr[0] > arr[1])
		{
			minmax.max = arr[0];
			minmax.min = arr[1];
		}
		else
		{
			minmax.max = arr[1];
			minmax.min = arr[0];
		}

		for (i = 2; i < n; i++)
		{
			if (arr[i] > minmax.max)
			{
				minmax.max = arr[i];
			}
			else if (arr[i] < minmax.min)
			{
				minmax.min = arr[i];
			}
		}
		return minmax;
	}

	// Driver Code
	public static void Main(String []args)
	{
		int []arr = {1000, 11, 445, 1, 330, 3000};
		int arr_size = 6;
		Pair minmax = getMinMax(arr, arr_size);
		Console.Write("Minimum element is {0}",
								minmax.min);
		Console.Write("\nMaximum element is {0}",
									minmax.max);
	}
}
<script>
// JavaScript program of above implementation

/* Class Pair is used to return two values from getMinMax() */
	function getMinMax(arr, n)
	{
		minmax = new Array();
		var i;
		var min;
		var max;

		/*If there is only one element then return it as min and max both*/
		if (n == 1) {
			minmax.max = arr[0];
			minmax.min = arr[0];
			return minmax;
		}

		/* If there are more than one elements, then initialize min
	and max*/
		if (arr[0] > arr[1]) {
			minmax.max = arr[0];
			minmax.min = arr[1];
		} else {
			minmax.max = arr[1];
			minmax.min = arr[0];
		}

		for (i = 2; i < n; i++) {
			if (arr[i] > minmax.max) {
				minmax.max = arr[i];
			} else if (arr[i] < minmax.min) {
				minmax.min = arr[i];
			}
		}

		return minmax;
	}

	/* Driver program to test above function */
	
		var arr = [1000, 11, 445, 1, 330, 3000];
		var arr_size = 6;
		minmax = getMinMax(arr, arr_size);
		document.write("\nMinimum element is " ,minmax.min +"<br>");
		document.write("\nMaximum element is " , minmax.max);
</script>

Output :
Minimum element is 1
Maximum element is 3000

Time Complexity: O(n)
Auxiliary Space: O(1) as no extra space was needed.

In this method, the total number of comparisons is 1 + 2(n-2) in the worst case and 1 + n – 2 in the best case.
In the above implementation, the worst case occurs when elements are sorted in descending order and the best case occurs when elements are sorted in ascending order.

Happy
Happy
100 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
100%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

2 thoughts on “How to find maximum and minimum of an array using Linear search ?

  1. Wow, fantastic weblog layout! How long have you been running a blog for?
    you made running a blog glance easy. The entire look of your site is wonderful, as neatly as the content!

Leave a Comment