Get the algorithm and the code in C language on how to find the minimum element in an array.
Finding the minimum of an array is the basis for more advanced sorting operations in DSA.
Algorithm on how to find the minimum element in an array
STEP 1: min=A[0]
STEP 2: FOR(I=1 TO N) DO
STEP 3: IF(min>A[i])DO
STEP 4: min=A[I]
STEP 6: ENDIF
STEP 7: ENDFOR
STEP 8: STOPCode for finding the minimum of an Array using C
Full code in C
#include <stdio.h>
int main()
{
int n;
printf("enter the limit ");
scanf("%d",&n);
int a[n];
printf("enter the elements ");
for(int i=0;i<n;i++)
{
printf("a[%d]",i);
scanf("%d",&a[i]);
}int min=a[0];
for(int i=0;i<n;i++)
{
if(min>a[i])
min=a[i];
}
printf("THE SMALLEST ELEMENT IS %d",min);
return 0;
}Explanation
- This C program is designed to read an array of integers from the user, find the smallest element in the array, and then print that smallest element. Let’s break down the code step by step:
#include <stdio.h>
int main()
{
int n;
printf("enter the limit ");
scanf("%d",&n);int n;: Declares a variable n to store the size of the array (how many elements will be input by the user).
printf("enter the limit ");: Asks the user to enter the number of elements (the “limit” of the array).
scanf("%d",&n);: Reads the number entered by the user and stores it in n, which will be the size of the array.
int a[n];
printf("enter the elements ");
for(int i=0; i<n; i++)
{
printf("a[%d]", i);
scanf("%d", &a[i]);
}
int a[n];: Declares an array a of size n.
This is a variable-length array (VLA), where the size of the array is determined at runtime based on the value of n.
printf("enter the elements ");: Asks the user to input the elements of the array.
for(int i=0; i<n; i++): This is a for loop that runs from i = 0 to i = n-1, allowing the user to input n elements into the array a.
printf("a[%d]", i);: Displays the indexito the user so they know where to input each element.scanf("%d", &a[i]);: Reads each integer entered by the user and stores it ina[i].
int min = a[0];
for(int i=0; i<n; i++)
{
if(min > a[i])
min = a[i];
}
int min = a[0];: Initializes min to the first element of the array a[0].
This will hold the smallest value found in the array.
for(int i=0; i<n; i++): This loop iterates through the array again to find the smallest element.
if(min > a[i]): Compares the current value ofminwith the elementa[i]at indexi. Ifa[i]is smaller than the currentmin, it updatesmintoa[i].
printf("THE SMALLEST ELEMENT IS %d", min);
return 0;
}
printf("THE SMALLEST ELEMENT IS %d", min);:
Once the loop finishes, the smallest element is printed. return 0;: The program returns 0 to indicate successful execution.
Summary
- It first asks the user for the number of elements (
n) in the array.
- Then it allows the user to input the
nelements into the array. - It finds the smallest element in the array by comparing each element with the current minimum.
- Finally, it prints out the smallest element found.
Output
user@computer$ : enter the limit 5
enter the elements a[0]2
a[1]3
a[2]1
a[3]3
a[4]5
THE SMALLEST ELEMENT IS 1Click here to get the algorithm and the code in C for merging two arrays.


