Finding Median of unsorted Array in linear time using C++ STL Last Updated : 15 Jul, 2025 Comments Improve Suggest changes 7 Likes Like Report 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: 4 Explanation: 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.5 Explanation: 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()) Below is the implementation of the above approach: CPP // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function for calculating // the median double 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 Code int 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; } Output: Median = 4.5 Time Complexity: O(N) Auxiliary Space Complexity: O(1) Create Quiz Comment C chirags_30 Follow 7 Improve C chirags_30 Follow 7 Improve Article Tags : C++ STL median-finding Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like