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

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:

`// 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Median = 4.5

**Time Complexity:** O(N)

**Auxillary Space Complexity:** O(1)

## Recommended Posts:

- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
- Program for Mean and median of an unsorted array
- Median of an unsorted array using Quick Select Algorithm
- Median of two sorted arrays of different sizes | Set 1 (Linear)
- Finding Median in a Sorted Linked List
- Find median of BST in O(n) time and O(1) space
- Find k most frequent in linear time
- Find a sorted subsequence of size 3 in linear time
- Sorted subsequence of size 3 in linear time using constant space
- Find Two Missing Numbers | Set 1 (An Interesting Linear Time Solution)
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Maximize the median of the given array after adding K elements to the same array
- Why is it faster to process sorted array than an unsorted array ?
- Maximize the median of an array
- Median of sliding window in an array
- k-th missing element in an unsorted array
- Remove duplicates from an unsorted array using STL in C++

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.