# Finding Median of an unsorted array in Linear Time using C++ standard library function

In this article, we will discuss how to find the median of an unsorted array without sorting it in linear time complexity with the help of standard library function nth_element()?
Submitted by Radib Kar, on July 11, 2020

Prerequisite: std::nth_element() in C++

Problem statement:

Find the median of an unsorted array.

What is median?

A median is a statistical measurement that divides an ordered set of data into two halves. Median is the partitioning point for any ordered list. In the case of a sorted array median is the middle element. If the array size is odd then the median is simply the middle element and if the array size is odd then the median is the average of the two middle elements.

Example:

Array with odd size:

```array= [5, 4, 3, 1, 2]

If the array was sorted then it would be [1,2,3,4,5]
and the middle element would be 3
Thus the median is 3
```

Array with even size:

```array= [5, 4, 3, 1, 2, 6]

If the array was sorted then it would be [1, 2, 3, 4, 5, 6]
and the middle element would be 3 & 4
Thus the median is (3+4)/2 = 3.5
```

So, to find the median of the unsorted array we need to find the middle element(s) when the array will be sorted. We don't require to sort the whole array, rather just need the middle element(s) if the array was sorted.

To achieve this we can use nth_element() function from the standard library which gives the nth_element() if the array was sorted.

So the algorithm would be:

If array size is odd:

We need the middle element only which is sorted_array[n/2] where n is the number of elements, and sorted_array is sorted version of the array. But we don't need to sport the array rather we will use like below,

```nth_element(array.begin(),array.begin()+n/2,array.end())
Where, array.begin()=start iterator to the array
array.end()=End iterator to the array

So the range is from start tio end the entire array
arr.begin()+n/2 = iterator to our desired nth element
After this array[n/2] will give us the middle element
if array was sorted which is median itself since size is odd.
```

If array size is even:

We need to middle elements which are n/2th element and (n/2-1)th element.

Just like previously, we did.

To find n/2th element

```nth_element(array.begin(),array.begin()+n/2,array.end())
store array[n/2] to variable a

To find (n/2-1)th element
nth_element(array.begin(),array.begin()+(n-1)/2,array.end())
store array[n/2] to variable b

So, the median would be (a+b)/2.0

Remember don't leave doing integer division
as that will lead to wrong answer. That's why divide by 2.0 not 2
```

Example:

```#include <bits/stdc++.h>
using namespace std;

//to print the vector
void print(vector<int> arr)
{
for (auto it : arr) {
cout << it << " ";
}
cout << endl;
}

int main()
{
int n;
cout << "Enter number of input elements \n";
cin >> n;

//to see how it's initialized
cout << "Input the elements\n";
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Printing the array initially...\n";
print(arr);

//array size odd
if (n % 2 == 1) {
//median is arr[n/2] if array is sorted
//can do the same using nth_element
nth_element(arr.begin(), arr.begin() + n / 2, arr.end());
cout << "Median is: " << arr[n / 2] << endl;
}
else { //array size even
//median is arr[n/2-1]+arr[n/2] if array is sorted

//n/2th element
nth_element(arr.begin(), arr.begin() + n / 2, arr.end());
int a = arr[n / 2];
//n/2-1 th element
nth_element(arr.begin(), arr.begin() + n / 2 - 1, arr.end());
int b = arr[n / 2 - 1];

cout << "Median is: " << (a + b) / 2.0 << endl;
}

return 0;
}
```

Output:

```RUN 1:
Enter number of input elements
5
Input the elements
5 4 3 1 2
Printing the array initially...
5 4 3 1 2
Median is: 3

RUN 2:
Enter number of input elements
6
Input the elements
6 5 4 1 2 3
Printing the array initially...
6 5 4 1 2 3
Median is: 3.5
```

Top MCQs

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates