C Program to Find Minimum Value in Array (original) (raw)
Last Updated : 20 Nov, 2024
In this article, we will learn how to find the minimum value in the array.
The easiest and straightforward method is to iterate through each element of the array, comparing each value to an assumed minimum and updating it if the current value is less.
C `
#include <stdio.h>
int findMin(int arr[], int n) {
// Assume first element as minimum
int min = arr[0];
for (int i = 1; i < n; i++) {
// Update m if arr[i] is smaller
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]);
// Find and print minimum value in arr
printf("%d\n", findMin(arr, n));
return 0;
}
`
This method is simply the linear search algorithm modified to find the minimum element.
There are also some other ways to find the minimum value in array in C. They are as follows:
Table of Content
Recursive Method
The function reduces the search size by comparing the last element with the minimum of the remaining elements and recursively processing the rest of the array. This continues until the base case is reached, where a single element remains, which is returned as the minimum.
C `
#include <stdio.h>
// Recursive approach to find the minimum element int findMin(int arr[], int n) {
// Base case: Only one element
if (n == 1) return arr[0];
// Find minimum from the rest of the array
int min = findMin(arr, n - 1);
// Return smaller element between curent element
// or minimum element in rest of the array
return arr[n - 1] < min ? arr[n - 1] : min;
}
int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]);
// Finding and printing the minimum element
printf("%d\n", findMin(arr, n));
return 0;
}
`
While this approach is intuitive, it can be less efficient than iterative method for large arrays due to the space required for recursive calls.
By Sorting
In an array sorted in ascending order, the minimum element is present at the beginning (0th index). The array can be sorted using qsort() function with a custom comparator for ascending order.
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);
}
int findMin(int arr[], int n) {
// Sort the array using qsort
qsort(arr, n, sizeof(int), compare);
// The first element is smallest after sorting
return arr[0];
}
int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]);
// Find and print the minimum element in arr
printf("%d\n", findMin(arr, n));
return 0;
}
`
This method is less efficient than linear search for just finding the minimum element but more suitable for finding the n-th smallest element.
Similar Reads
- How to Find Maximum Value in an Array in C? In C, arrays are data structures that allow the user to store a collection of data of the same type. In this article, we will learn how we can find the maximum value in an array in C. Example Input: arr = {5,3,1,2,4} Output: The maximum value of the array is: 5Finding Maximum Value in an Array in CW 2 min read
- C Program to Find the Maximum and Minimum Element in the Array 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 curre 3 min read
- C Program For Maximum and Minimum of an Array Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons. Examples: Input: arr[] = {3, 5, 4, 1, 9}Output: Minimum element is: 1Â Â Â Â Â Â Â Maximum element is: 9 Input: arr[] = {22, 14, 8, 17, 35, 3}Output:Â Minimum eleme 7 min read
- C Program for Ceiling in a sorted array Given a sorted array and a value x, the ceiling of x is the smallest element in array greater than or equal to x, and the floor is the greatest element smaller than or equal to x. Assume than the array is sorted in non-decreasing order. Write efficient functions to find floor and ceiling of x. Examp 4 min read
- How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read
- C Program to Sort an Array in Ascending Order Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C 3 min read
- How to Find the Mode of Numbers in an Array in C? In C, the mode of array numbers is the element that appears most frequently in the array. To find the mode, we can count the occurrences of each element and identify the one with the highest count. In this article, we will find the mode of numbers in C. Example:Input: myArray = { 1, 2, 3, 4, 5, 2, 3 5 min read
- How to Find the Mode of Numbers in a Sorted Array in C? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7} 2 min read
- C Program to Implement Min Heap In this article, we will learn the implementation of min heap in C programming language. A heap is a data structure like a tree with some special properties. The basic requirement of the heap is that the value of a node must be greater than equal to (or smaller than equal to) the value of its childr 10 min read
- How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read