
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum Element in an Array Using Linear Search in C++
Linear search is a sequential searching algorithm where we traverse every element within the input array and compare it with the key element to be found. The minimum element refers to the smallest element in the array.
In this article, we have an array of integers. Our task is to find the minimum element in that array using linear search. Here are the three approaches you can use to find the minimum element:
Using Value
In this approach, we will be using the value of the array to find the minimum element of an array using linear search. At the beginning, we assign the starting element as the minimum value to the minimum. Then we compare this value with each element of the array and replace the value if we find a value less than the existing element in the minimum.
Example
In this example, we have an unsorted array and we are using the array value to find the minimum element of the array:
#include<iostream> using namespace std; int main() { int i, minimum; int a[10] = {12, 6, 7, 9, 16, 15, 59, 2, 30, 1}; int n = sizeof(a) / sizeof(a[0]); minimum = a[0]; //using array value cout<<"\nThe data element of array:"; for(i = 0; i < n; i++) { cout<<" "<<a[i]; // Linear Search if(minimum > a[i]) minimum= a[i]; } cout<<"\nMinimum Element: "<<minimum; return 0; }
The output of the above code is given below:
The data element of array: 12 6 7 9 16 15 59 2 30 1 Minimum Element: 1
Using Index
This approach uses index value to search for the minimum element in the array. It is similar to the previous approach, the only difference is that we have used index instead of array value in this approach.
Example
The following example uses an index to find the minimum in the array using linear search:
#include <iostream> using namespace std; int minEle(int arr[], int n) { int minIndex = 0; for(int i = 1; i < n; i++) { //Using index to compare if(arr[i] < arr[minIndex]) { minIndex = i; } } return arr[minIndex]; } int main() { int arr[] = {7, 2, 8, 4, 1}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Array elements: "; for(int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << "\nMinimum element: " << minEle(arr, n); return 0; }
The output of the above code is given below:
Array elements: 7 2 8 4 1 Minimum element: 1
Using Pointer
In this approach, we have used a pointer to traverse the array and find the minimum element. We have assigned the first element as the minimum. Then we traverse through each element of the array using a pointer and compare the current element with minimum. We update the minimum, if a smaller element is found.
Example
In this example, we are using a pointer to compare each element and find the minimum value in the array:
#include <iostream> using namespace std; int minEle(int* arr, int n) { int min = *arr; // Initializing min with the first element for(int i = 1; i < n; i++) { // Using pointer to compare if(*(arr + i) < min) { min = *(arr + i); } } return min; } int main() { int arr[] = {52, 57, 40, 32, 43}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Array elements: "; for(int i = 0; i < n; i++) { cout << *(arr + i) << " "; } cout << "\nMinimum element: " << minEle(arr, n); return 0; }
The output of the above code is given below:
Array elements: 52 57 40 32 43 Minimum element: 32