Finding the median of an unsorted array

Luv picture Luv · May 19, 2012 · Viewed 116.4k times · Source

To find the median of an unsorted array, we can make a min-heap in O(nlogn) time for n elements, and then we can extract one by one n/2 elements to get the median. But this approach would take O(nlogn) time.

Can we do the same by some method in O(n) time? If we can, then please tell or suggest some method.

Answer

Sergey Kalinichenko picture Sergey Kalinichenko · May 19, 2012

You can use the Median of Medians algorithm to find median of an unsorted array in linear time.