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

«
»

C++ Program to find the minimum element of an array using Linear Search approach.

Problem Description

1. Search the minimum element with time complexity O(n).

Problem Solution

1. Compare the element at the beginning with another array element sequentially.
2. Swap values if the element at the beginning is larger than the other element.
3. This value will be the minimum value among the given data.
4. Exit.

advertisement
Program/Source Code

C++ program to find the minimum element of an array using Linear Search approach.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

#include<iostream>
 
using namespace std;
 
int main()
{
	int n, i, min, a[30]={89, 53, 95, 12, 9, 67, 72, 66, 75, 77, 18, 24, 35, 90, 38, 41, 49, 81, 27, 97, 111, 116, 854, 234, 658, 546, 987, 268, 946, 852};
	char ch;
	min = a[0];
	cout<<"\nThe data element of array:";
 
	for(i = 1; i < 30; i++)
	{
		cout<<" "<<a[i];
		// Assign min to the current element if its value is lesser than min value.
		if(min > a[i])
			min = a[i];
	}
	cout<<"\n\nMinimum of the data elements of array using linear search is: "<<min;
 
    return 0;	
}
Program Explanation

1. Assign the data element to an array.
2. Assign the value at ‘0’ index to min variable.
3. Compare min with other data element sequentially.
4. Swap values if min value is more then the value at that particular index of the array.
5. Display the minimum value.
6. Exit.

advertisement
advertisement
Runtime Test Cases
The data element of array: 89 53 95 12 9 67 72 66 75 77 18 24 35 90 38 41 49 81 27 97 111 116 854 234 658 546 987 268 946 852
 
Minimum of the data elements of array using linear search is: 9

Sanfoundry Global Education & Learning Series – C++ Algorithms.
To practice all C++ Algorithms, here is complete set of 1000 C++ Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.