C Program to Find Minimum Element in an Array using Linear Search


This is a C Program to implement Linear Search Algorithm to find the minimum element in an array.

Problem Description

We have to create a C Program which finds the minimum element in an array using Linear Search Algorithm. We have to create a linear search function which will have array and its size as parameters and return the element whose value is smallest in the array.

Expected Input and Output

1. Average case: When the array size is more than 1 we have to traverse the whole array to find the minimum element.

If the input array has the data as {4, 6, 1, 2, 5, 3}
then the minimum element in this array will be 1

Average case time complexity: O(n)

2. Best Case: When the array has only one element, so that will be the minimum.

If the input array has the data as {-3}
then the minimum element in this array will be -3

Best case time complexity: O(1)

Problem Solution

1. We first have to create an array of numbers by taking input from user. We have to input an array of numbers and then apply the linear search algorithm to find the minimum value element in an array.
2. In order to find out the minimum element from the array of numbers we assume the first element of an array to be the minimum.
3. After that we traverse the whole array from the beginning and if we encounter any element whose value is less than the value of the first element we’ll put that element as minimum element.
4. This process will continue till we have compared all the elements with each other. As a last step we return the smallest element from the array.

Program/Source Code

Here is source code of the C Program to find the minimum element in an array using Linear Search Algorithm. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

  1. /* C program to find the minimum element in an array using Linear Search */
  2. #include <stdio.h>
  3. int min_linearsearch(int numbers[], int n)
  4. {
  5.     int min = numbers[0];
  6.     int i;
  7.     for (i = 1; i <= n; i++)
  8.     {
  9.         if (min > numbers[i])
  10.             min = numbers[i];
  11.     }
  12.     return min;
  13. }
  14. int main() {
  15.     int n;
  16.     printf("Enter number of elements in array: ");
  17.     scanf("%d",&n);
  18.     int numbers[n];
  19.     int i;
  20.     int min ;
  21.     printf("Enter %d numbers : ", n);
  22.     printf("\n");
  23.     for (i = 0; i < n; i++)
  24.     {
  25.         scanf("%d", &numbers[i]);
  26.     }
  27.     min = min_linearsearch(numbers,n);
  28.     printf("\nMinimum number in the array is : %d\n", min);
  29.     return 0;
  30.     }
Program Explanation

1. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found.
2. The array is searched sequentially and the position is returned if the key element to be searched is available in the array, otherwise -1 is returned.
3. Here in this C Program we have first created the array, after that we have assumed first element of array to be minimum.
4. We traverse the whole array and compare that element with rest of the elements in array and return the smallest element in the array using function min_linearsearch(int numbers[]).

Runtime Test Cases
1. Enter number of elements in array: 6
   Enter 6 numbers :
   Minimum number in the array is : 1
2. Enter number of elements in array: 1
   Enter 1 numbers :
   Minimum number in the array is : -3

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.


Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn