Finding Median of unsorted Array in linear time using C++ STL - GeeksforGeeks

07 Aug, 2020
Given an unsorted array arr[] having N elements, the task is to find out the median of the array in linear time complexity.
Examples:
Input: N = 5, arr[] = {4, 1, 2, 6, 5}Output: 4Explanation:Since N = 5, which is odd, therefore the median is the 3rd element in the sorted array.The 3rd element in the sorted arr[] is 4.Hence the median is 4.
Input: N = 8, arr[] = {1, 3, 4, 2, 6, 5, 8, 7}Output: 4.5Explanation:Since N = 8, which is even, therefore median is the average of 4th and 5th element in the sorted array.The 4th and 5th element in the sorted array is 4 and 5 respectively.Hence the median is (4+5)/2 = 4.5.
Approach: The idea is to use nth_element() function in C++ STL.
If the number of element in the array is odd, then find the (N/2)th element using nth_element() function as illustrated below and then the value at index (N/2) is the median of the given array.nth_element(arr.begin(), arr.begin() + N/2, arr.end())Else find the (N/2)th and ((N – 1)/2)th element using nth_element() function as illustrated below and find the average of the values at index (N/2) and ((N – 1)/2) is the median of the given array.nth_element(arr.begin(), arr.begin() + N/2, arr.end())nth_element(arr.begin(), arr.begin() + (N – 1)/2, arr.end())
If the number of element in the array is odd, then find the (N/2)th element using nth_element() function as illustrated below and then the value at index (N/2) is the median of the given array.nth_element(arr.begin(), arr.begin() + N/2, arr.end())
nth_element(arr.begin(), arr.begin() + N/2, arr.end())
Else find the (N/2)th and ((N – 1)/2)th element using nth_element() function as illustrated below and find the average of the values at index (N/2) and ((N – 1)/2) is the median of the given array.nth_element(arr.begin(), arr.begin() + N/2, arr.end())nth_element(arr.begin(), arr.begin() + (N – 1)/2, arr.end())
nth_element(arr.begin(), arr.begin() + N/2, arr.end())nth_element(arr.begin(), arr.begin() + (N – 1)/2, arr.end())
Below is the implementation of the above approach:
// C++ program for the above approach  #include <bits/stdc++.h>using namespace std;  // Function for calculating// the mediandouble findMedian(vector<int> a,                  int n){      // If size of the arr[] is even    if (n % 2 == 0) {          // Applying nth_element        // on n/2th index        nth_element(a.begin(),                    a.begin() + n / 2,                    a.end());          // Applying nth_element        // on (n-1)/2 th index        nth_element(a.begin(),                    a.begin() + (n - 1) / 2,                    a.end());          // Find the average of value at        // index N/2 and (N-1)/2        return (double)(a[(n - 1) / 2]                        + a[n / 2])               / 2.0;    }      // If size of the arr[] is odd    else {          // Applying nth_element        // on n/2        nth_element(a.begin(),                    a.begin() + n / 2,                    a.end());          // Value at index (N/2)th        // is the median        return (double)a[n / 2];    }}  // Driver Codeint main(){    // Given array arr[]    vector<int> arr = { 1, 3, 4, 2,                        7, 5, 8, 6 };      // Function Call    cout << "Median = "         << findMedian(arr, arr.size())         << endl;    return 0;}
Median = 4.5

Time Complexity: O(N)Auxiliary Space Complexity: O(1)
nidhi_biet
median-finding
STL
Algorithms
Arrays
C++
C++ Programs
Competitive Programming
Mathematical
Write From Home
Arrays
Mathematical
STL
Algorithms
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
How to Start Learning DSA?
Difference between Algorithm, Pseudocode and Program
K means Clustering - Introduction
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Arrays in Java
Arrays in C/C++
Maximum and minimum of an array using minimum number of comparisons
Write a program to reverse an array or string
Program for array rotation