Find Minimum and Maximum Element in an Array When working with arrays, it’s common to need to find the min and max values contained within the array. In this article, we’ll explore several different methods for finding the min and max values in an array using various programming languages.
Understanding Arrays
Before we dive into how to find min and max values in an array, let’s first take a moment to understand what an array is. An array is a collection of data elements of the same type that are stored together in contiguous memory locations. These data elements can be accessed using an index or a key.
Arrays are widely used in programming for storing and manipulating data. They are a fundamental data structure and are supported in nearly every programming language.
Find minimum and maximum element in an array
Method 1: Linear Search
C Code To Find Min and Max Value in Array
#include <stdio.h>
int main() {
int myArray[] = {5, 7, 2, 9, 1, 4, 6, 8, 3};
int arraySize = sizeof(myArray) / sizeof(myArray[0]);
int min = myArray[0];
int max = myArray[0];
for(int i = 1; i < arraySize; i++) {
if(myArray[i] < min) {
min = myArray[i];
}
if(myArray[i] > max) {
max = myArray[i];
}
}
printf("Min value: %dn", min);
printf("Max value: %dn", max);
return 0;
}
Find Min and Max Value in Array In C++
#include <iostream>
#include <array>
int main() {
std::array<int, 9> myArray = {5, 7, 2, 9, 1, 4, 6, 8, 3};
int min = myArray[0];
int max = myArray[0];
for(int i = 1; i < myArray.size(); i++) {
if(myArray[i] < min) {
min = myArray[i];
}
if(myArray[i] > max) {
max = myArray[i];
}
}
std::cout << "Min value: " << min << std::endl;
std::cout << "Max value: " << max << std::endl;
return 0;
}
Find Min and Max Value in Array In Python
my_array = [5, 7, 2, 9, 1, 4, 6, 8, 3]
min = my_array[0]
max = my_array[0]
for i in range(1, len(my_array)):
if my_array[i] < min:
min = my_array[i]
if my_array[i] > max:
max = my_array[i]
print("Min value:", min)
print("Max value:", max)
Time and Space Complexity
The time complexity of the linear search approach is O(n). In the worst case, the algorithm will need to make n-1 comparisons to find the minimum or maximum element. The space complexity of this approach is O(1), since we only need to allocate space for two variables: min and max.
Method 2: Sorting Algorithm
Find Min and Max Value in Array In C
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = {12, 45, 1, 98, 78, 36};
int n = sizeof(arr) / sizeof(arr[0]);
int i, j, temp;
// Sorting the array using bubble sort algorithm
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Min value in the array: %dn", arr[0]);
printf("Maximum value in the array: %dn", arr[n-1]);
return 0;
}
This code initializes an array of integers and finds its length using the sizeof
operator. Then, it uses the bubble sort algorithm to sort the array in ascending order. Bubble sort compares adjacent elements of the array and swaps them if they are in the wrong order. It repeats this process until the array is fully sorted.
After sorting the array, the min value is the first element (arr[0]
) and the maximum value is the last element (arr[n-1]
), where n
is the length of the array.
Find Min and Max Value in Array In C++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {12, 45, 1, 98, 78, 36};
int n = sizeof(arr) / sizeof(arr[0]);
// Sorting the array using sort function
sort(arr, arr+n);
cout << "Min value in the array: " << arr[0] << endl;
cout << "Maximum value in the array: " << arr[n-1] << endl;
return 0;
}
This code is similar to the C code, but it uses the sort
function from the <algorithm>
header to sort the array. The sort
function takes two iterators as arguments: the beginning and end of the range to be sorted. In this case, we pass arr
and arr+n
, where n
is the length of the array.
Find Min and Max Value in Array In Python
arr = [12, 45, 1, 98, 78, 36]
# Sorting the array using sorted function
arr = sorted(arr)
print("Minimum value in the array:", arr[0])
print("Maximum value in the array:", arr[-1])
This code initializes a list of integers and uses the sorted
function to sort the list in ascending order. The sorted
function returns a new sorted list, so we assign it back to the arr
variable.
After sorting the list, the minimum value is the first element (arr[0]
) and the maximum value is the last element (arr[-1]
), which is the same as arr[len(arr)-1]
.
Method 3: Using Built In Functions
Find Min and Max Value in Array In C
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int arr[] = { 12, 45, 1, 98, 78, 36 };
int n = sizeof(arr) / sizeof(arr[0]);
int min = INT_MAX, max = INT_MIN;
// Finding the minimum and maximum values in the array using built-in functions
for (int i = 0; i < n; i++) {
if (arr[i] < min)
min = arr[i];
if (arr[i] > max)
max = arr[i];
}
printf("Minimum value in the array: %dn", min);
printf("Maximum value in the array: %dn", max);
return 0;
}
C Code Explanation:
- First, we initialize an array of integers called ‘arr’ with some values.
- We also find the length of the array ‘n’ using the sizeof() function.
- Then, we initialize two variables ‘min’ and ‘max’ to the maximum and minimum integer values respectively, using the constants INT_MAX and INT_MIN from the limits.h header file.
- We then loop through the array using a for loop and check if each element is smaller than the current value of ‘min’ or greater than the current value of ‘max’.
- If a smaller element is found, we update ‘min’ to that value.
- Similarly, if a greater element is found, we update ‘max’ to that value.
- Finally, we print out the values of ‘min’ and ‘max’ to the console.
Find Min and Max Value in Array In C++
#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;
int main() {
int arr[] = { 12, 45, 1, 98, 78, 36 };
int n = sizeof(arr) / sizeof(arr[0]);
// Finding the minimum and maximum values in the array using built-in functions
int min = *min_element(arr, arr + n);
int max = *max_element(arr, arr + n);
cout << "Minimum value in the array: " << min << endl;
cout << "Maximum value in the array: " << max << endl;
return 0;
}
C++ Code Explanation:
- We start by initializing an array ‘arr’ with some values.
- We find the length of the array ‘n’ using the sizeof() function.
- We then make use of the min_element and max_element functions to find the minimum and maximum elements in the array respectively.
- These functions return iterators pointing to the minimum and maximum elements in the array.
- We then dereference these iterators using the * operator to get the actual values of the minimum and maximum elements.
- Finally, we print out the values of the minimum and maximum elements to the console.
Find Min and Max Value in Array In Python
arr = [12, 45, 1, 98, 78, 36]
# Finding the minimum and maximum values in the array using built-in functions
min_val = min(arr)
max_val = max(arr)
print("Minimum value in the array: ", min_val)
print("Maximum value in the array: ", max_val)
Python Code Explanation:
- We start by initializing an array ‘arr’ with some values.
- We then make use of the built-in min and max functions in Python to find the minimum and maximum values in the array respectively.
- These functions return the minimum and maximum values directly.
- We store the minimum and maximum values in two variables called ‘min_val’ and ‘max_val’ respectively.
- Finally, we print out the values of ‘min_val’ and ‘max_val’ to the console.
Conclusion
In this article, we’ve explored several different methods for finding the min and max values in an array using various programming languages. While some methods may be more efficient than others, it’s important to understand the different approaches and their strengths and weaknesses.