C Program to Find the Maximum and Minimum Element in the Array (original) (raw)

Last Updated : 20 Nov, 2024

In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.

The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the current element is smaller or larger respectively.

C `

#include <stdio.h>

// Function to find maximum and minimum in an array void findMinMax(int arr[], int n, int *max, int *min) {

// Assuming first element as minimum and maximum
*max = arr[0];
*min = arr[0];

for (int i = 1; i < n; i++) {

    // Update max if arr[i] is larger
    if (arr[i] > *max)
        *max = arr[i];

    // Update min if arr[i] is smaller
    if (arr[i] < *min)
        *min = arr[i];
}

}

int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); int max, min;

  // Finding minimum and maximum values in arr
findMinMax(arr, n, &max, &min);

printf("%d\n", max);
printf("%d\n", min);
return 0;

}

`

**Explanation: We passed the variables to store the minimum and maximum element to findMinMax() function as to avoid the complex techniques to pass multiple values from function.

C also have a few other methods to find the maximum and minimum element of the array that are useful in different cases. Let’s take a look at them one by one:

Table of Content

Using Recursion

In this approach, we search for the maximum and minimum values by checking each element the array using recursion.

C `

#include <stdio.h>

// Recursive function to find maximum and minimum void findMinMax(int arr[], int n, int *max, int *min) {

// Base Case: Only single element left
if (n == 1) {
    *max = arr[0];
    *min = arr[0];
    return;
}

// Recursive call for the rest of the array
findMinMax(arr, n - 1, max, min);

// Update max and min for the current element
if (arr[n - 1] > *max)
    *max = arr[n - 1];
if (arr[n - 1] < *min)
    *min = arr[n - 1];

}

int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); int max, min;

// Call the recursive function
findMinMax(arr, n, &max, &min);

printf("%d\n", max);
printf("%d", min);
return 0;

}

`

By Sorting

The largest and smallest element can be easily determined by sorting the array using qsort() function. If the array is sorted in ascending order, the smallest element will be at the beginning (0-th index), and the largest will be at the end (size - 1 index) of the array.

C `

#include <stdio.h> #include <stdlib.h>

// Comparator function for qsort (ascending order) int compare(const void *a, const void b) { return ((int *)a - *(int *)b); }

void findMinMax(int arr[], int n, int *max, int *min) {

  // Sort the array using qsort
qsort(arr, n, sizeof(int), compare);

  // Minimum element at the start
  *min = arr[0];
  
  // Maximum element at the end
  *max = arr[n - 1];

}

int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); int min, max;

  // Finding minimum and maximum element in the arr
  findMinMax(arr, n, &min, &max);

// After sorting, the first element is the smallest
  // and the last element is the largest
printf("%d\n", min);
printf("%d", max);

return 0;

}

`

This method is simple to implement but has a higher time complexity.

Similar Reads