C Program to Find Minimum Element in an Array

• Write a C program to find minimum element of an array

Given an array of length N, we have to find the minimum element of an array and it's position in array. If minimum element occurs more than once in input array, then we will return the index of it's first occurrence(smallest index). We can find the minimum element in an array by traversing the array from first to last element and comparing each element with the minimum element found till now.

Algorithm to find minimum element of array
• First of all, take N numbers as input from user and store it in an array(lets call it inputArray).

• We will declare variable minElement and minElementPosition and initialize it with first element of inputArray and 0 respectively.

• We will start traversing inputArray from index 0 to N -1 and compare each element with minElement. If minElement is more than current element, we will update minElement and minElementPosition with current element and current position respectively.

• At any instant of time suppose we are at index i, then minElement will give the minimum element between array index 0 to i.
Time Complexity : O(n)

C program to find minimum element and it's position in array

In below program, we first take number of elements as input from user using scanf function and stores in an integer variable 'elementCount'. Then we take 'elementCount' numbers as input from user and stores them in an integer array using a for loop. Now, we assume that first element(inputArray) is the minimum element of inputArray and starts traversing inputArray form index 0 to N-1. For every element we compare it's value with minElement and update value of minElement If current element is greater than minElement. At the end of traversal, minElement will contain the minimum element of inputArray and minElementPosition will contain it's position in array.

```/*
* C Program to find minimum element in an array
*/

#include <stdio.h>
#include <conio.h>

int main(){
int minElement, inputArray, minElementPosition;
int elementCount, counter;

printf("Enter number of elements in array: ");
scanf("%d", &elementCount);
printf("Enter %d numbers \n", elementCount);

for(counter = 0; counter < elementCount; counter++){
scanf("%d", &inputArray[counter]);
}
/* Initializing minElement and minElementPosition
* with first element of inputArray and zero
* respectively
*/
minElement = inputArray;
minElementPosition = 0;

/* Compare each element of input array with minElement, and
*  if it is less than minElement update minElement and
*  minElementPosition
*/
for(counter = 1; counter < elementCount; counter++){
if(inputArray[counter] < minElement){
minElement = inputArray[counter];
minElementPosition = counter;
}
}
/* Print Minimum element and it's position in array */
printf("Minimum element in array is %d at index %d",
minElement, minElementPosition);

getch();
return 0;
}
```

Program Output
```Enter number of elements in array: 6
Enter 6 numbers
4 -2 7 1 -4 8
Minimum element in array is -4 at index 4
```

C program to find minimum elements in an array using divide and conquer

```/*
* C Program to find minimum element in an array
* using Divide and Conquer
* @author  Tech Crash Course
*/
#include <stdio.h>
#include <conio.h>

int getMin(int num1, int num2);
int getMinElement(int *array, int leftIndex, int rightIndex);

int main(){
int minElement, inputArray;
int elementCount, counter;

printf("Enter number of elements in array: ");
scanf("%d", &elementCount);
printf("Enter %d numbers \n", elementCount);

for(counter = 0; counter < elementCount; counter++){
scanf("%d", &inputArray[counter]);
}
minElement = getMinElement(inputArray, 0, elementCount-1);
printf("Minimum element in array is %d ", minElement);

getch();
return 0;
}

/*
* Function to find min of two number
*/
int getMin(int num1, int num2){
if(num1 <= num2)
return num1;
else
return num2;
}

/*
*  Function to find minElement of Array using Divide and Conquer
*/
int getMinElement(int *array, int leftIndex, int rightIndex){
int midIndex, leftMin, rightMin;
if(NULL == array){
printf("Invalid Input");
return -1;
}
/*  exit condition, for sub-Array of size 1  */
if(leftIndex == rightIndex)
return array[leftIndex];
/* Divide input array in two equal half
* find min Element of left and right half of Array
* return minimum of leftMin and rightMin
*/
midIndex = (leftIndex + rightIndex) / 2;
leftMin = getMinElement(array, leftIndex, midIndex);
rightMin = getMinElement(array, midIndex+1, rightIndex);

return getMin(leftMin, rightMin);
}
```

Program Output
```Enter number of elements in array: 6
Enter 6 numbers
1 9 2 2 0 6
Minimum element in array is 0
```
Related Topics
 C Program to find maximum elements in an array C program to find second largest element in array C program to delete an element from an array C program to delete duplicate elements from a sorted array C program to delete duplicate elements from an array C program to find scalar multiplication of a matrix C program to reverse a number using recursion C program to reverse an array List of all C Programs