
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 of an Array using Binary Search in C++
The binary search works on the divide and conquer principle as it keeps dividing the array into half before searching. For applying the binary search algorithm, the given array should be sorted. Since the array is sorted we do not need to search the minimum element in the array.
In this article, the given array has strictly decreasing elements in the left sub-array till it reaches the minimum element and the right sub-array has strictly increasing elements. For example: {40, 30, 20, 10, 25, 35}. In this example, the array is decreasing till it reaches the minimum element (10) and then starts increasing.
Example
The following example finds the minimum element present in a the given array using binary search algorithm:
Input: array = {50, 40, 30, 20, 25, 35, 45, 55, 65, 75} Output: Minimum element is: 20
Steps to Find Minimum Element in Array using Binary Search
The steps to find the minimum element in the given array using binary search are as follows:
- First, we declare an array of 10 elements. Then we have used two pointer variables low and high, that point to the starting and the last element of the array.
- Then, we find the mid element and compare the mid element with its adjacent elements.
- If the mid element is smaller than its adjacent elements (left and right), then it is the minimum element of the array.
- If the mid element is not the lowest element in the previous step, then we check if the mid element is greater than the (mid-1)th element. If this condition is satisfied, then we search in the left sub-array and set the high pointer to mid - 1.
- Otherwise, we search the min element in the right sub-array if the min element is not found in the above two steps.
- We repeat the above 4 steps until the minimum element is found or while low <= high.
C++ Program to Find Minimum Element in Array using Binary Search
Here is the code implementation of the above steps where we have used the binary search to find the minimum element in the given array:
#include <iostream> using namespace std; int minEle(int arr[], int n) { int low = 0, high = n - 1; while (low <= high) { int mid = low + (high - low) / 2; // Checking the adjacent elements of mid element // to check if the mid element is the minimum if ((mid == 0 || arr[mid] < arr[mid - 1]) && (mid == n - 1 || arr[mid] < arr[mid + 1])) { return arr[mid]; } // Searching in left half if (mid > 0 && arr[mid - 1] < arr[mid]) { high = mid - 1; } // Searching in right sub-array else { low = mid + 1; } } return -1; } int main() { int arr[] = {50, 40, 30, 20, 25, 35, 45, 55, 65, 75}; int n = sizeof(arr) / sizeof(arr[0]); cout << "The given array is: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } int minElement = minEle(arr, n); cout << "\nMinimum element is: " << minElement << endl; return 0; }
The output of the above code is as follows:
The given array is: 50 40 30 20 25 35 45 55 65 75 Minimum element is: 20