Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the median of an unsorted array without sorting [duplicate]

is there a way to find the Median of an unsorted array: 1- without sorting it. 2- without using the select algorithm, nor the median of medians

I found a lot of other questions similar to mine. But the solutions, most of them, if not all of them, discussed the SelectProblem and the MedianOfMedians

like image 980
Monica Avatar asked Nov 27 '15 20:11

Monica


People also ask

How do you find the median of an unsorted array without sorting?

This Problem Can be done is a linear Time O(N),where N=A. length() . Yes ,Selection Algorithm Finds the Median of an unsorted Array without Sorting it. The Selection Algorithm uses the concept of Quick Sort[But does not actually sort the array though] ,especially the partition Steps.

Can you find the median without sorting?

You can certainly find the median of an array without sorting it. What is not easy is doing that efficiently. For example, you could just iterate over the elements of the array; for each element, count the number of elements less than and equal to it, until you find a value with the correct count.

Can you find median of unsorted array?

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.

How do you find the median of an unsorted list?

To find median: First, simply sort the array. Then, check if the number of elements present in the array is even or odd. If odd, then simply return the mid value of the array. Else, the median is the average of the two middle values.


2 Answers

You can certainly find the median of an array without sorting it. What is not easy is doing that efficiently.

For example, you could just iterate over the elements of the array; for each element, count the number of elements less than and equal to it, until you find a value with the correct count. That will be O(n2) time but only O(1) space.

Or you could use a min heap whose size is just over half the size of the array. (That is, if the array has 2k or 2k+1 elements, then the heap should have k+1 elements.) Build the heap using the first array elements, using the standard heap building algorithm (which is O(N)). Then, for each remaining element x, if x is greater than the heap's minimum, replace the min element with x and do a SiftUp operation (which is O(log N)). At the end, the median is either the heap's minimum element (if the original array's size was odd) or is the average of the two smallest elements in the heap. So that's a total of O(n log n) time, and O(n) space if you cannot rearrange array elements. (If you can rearrange array elements, you can do this in-place.)

like image 173
rici Avatar answered Oct 21 '22 11:10

rici


There is a randomized algorithm able to accomplish this task in O(n) steps (average case scenario), but it does involve sorting some subsets of the array. And, because of its random nature, there is no guarantee it will actually ever finish (though this unfortunate event should happen with vanishing probability).

I will leave the main idea here. For a more detailed description and for the proof of why this algorithm works, check here.

Let A be your array and let n=|A|. Lets assume all elements of A are distinct. The algorithm goes like this:

  1. Randomly select t = n^(3/4) elements from A.
  2. Let T be the "set" of the selected elements.Sort T.
  3. Set pl = T[t/2-sqrt(n)] and pr = T[t/2+sqrt(n)].
  4. Iterate through the elements of A and determine how many elements are less than pl (denoted by l) and how many are greater than pr (denoted by r). If l > n/2 or r > n/2, go back to step 1.
  5. Let M be the set of elements in A in between pl and pr. M can be determined in step 4, just in case we reach step 5. If the size of M is no more than 4t, sort M. Otherwise, go back to step 1.
  6. Return m = M[n/2-l] as the median element.

The main idea behind the algorithm is to obtain two elements (pl and pr) that enclose the median element (i.e. pl < m < pr) such that these two are very close one two each other in the ordered version of the array (and do this without actually sorting the array). With high probability, all the six steps only need to execute once (i.e. you will get pl and pr with these "good" properties from the first and only pass through step 1-5, so no going back to step 1). Once you find two such elements, you can simply sort the elements in between them and find the median element of A.

Step 2 and Step 5 do involve some sorting (which might be against the "rules" you've mysteriously established :p). If sorting a sub-array is on the table, you should use some sorting method that does this in O(slogs) steps, where s is the size of the array you are sorting. Since T and M are significantly smaller than A the sorting steps take "less than" O(n) steps. If it is also against the rules to sort a sub-array, then take into consideration that in both cases the sorting is not really needed. You only need to find a way to determine pl, pr and m, which is just another selection problem (with respective indices). While sorting T and M does accomplish this, you could use any other selection method (perhaps something rici suggested earlier).

like image 36
cobarzan Avatar answered Oct 21 '22 12:10

cobarzan