To find the median of an unordered array

Source: Internet
Author: User

The median is the element that is in the middle of the array after the order is sorted. The array length is not considered as an even number case. Sets the number of elements in the collection to N.

The simple thought of the following:
Idea 1) Order the unordered array and take out the intermediate elements
Time complexity using the ordinary comparison sorting method O (N*LOGN)
In the case of a non-comparative counting sort method, the time complexity O (n) and the spatial complexity are O (n).

Idea 2)
2.1) Adjust the front (n+1)/2 elements to a small top heap,
2.2) For each subsequent element, and heap top comparison, if less than equals heap top, discard it, remove an element. If it is larger than the top of the heap, replace the heap top with that element, adjust the heap, and remove an element. Repeat 2.2 steps
2.3) When all elements are traversed, the top of the heap is the median.

Idea 3)cooked to say, want to let the algorithm run faster, with split treatment!
Fast sorting is named "Quick Platoon", is not the wave of fame! Because the fast platoon is a sort of sorting Method!
In the same way, finding the median can also be used in the idea of quick-sorting. Specific as follows:
Pick an element arbitrarily, to change the element as the fulcrum, divides the set into two parts, if the left set length is exactly (n-1)/2, then the fulcrum is just the median number. If the left length < (n-1)/2, then the median point is on the right, whereas the median is on the left. Go to the appropriate side and continue looking for the median point.
This method is fast, but in the worst case the time complexity is O (n^2), but the average time complexity seems to be O (N).

Idea 4) There is uncertainty in the method of the quick-row, which makes the worst and the best time difference is very big, then there is a definite method? The answer is yes.
It seems to be in the introduction of algorithms. Here I will not delve into, can refer to the following article,
O (n) Time Quick selection
http://www.shadowxh.com/?p=598
And the comments of others in this article

Extension One:
Finds the K-small element in n elements (from a programming Zhu Ji Nanxiong)

Programming Zhu Ji Nanxiong gives a solution to the complexity of Time O (N). The scheme is adapted from a quick sort.
After a quick row of division,
1) If the length of the left half is >k-1, then this element must be in the left half.
2) If the length of the left half is ==k-1, then the current dividing element is the result.
3) If ..... <k-1, then this element must be in the right half.
Furthermore, the method can be implemented by using tail recursion. Higher efficiency.

Time complexity analysis, because almost every time the sequence is divided into half ... The hypothetical elements are randomly optimized, and the time complexity is approximate to
N+n/2+n/4 .... = 2n* (1-2^-(Logn)) when N is larger it is approximately equal to 2N, which is O (N).

It seems that the use of fast-track can be big ...

It is also used to find the first k small elements in n elements, the first k large elements .... Wait a minute.


Extension two:
Find the K-small element in n elements, assuming that memory is limited, only k/4 elements can be contained.
Split-Trip lookup,
The first trip, the heap method to find the smallest K/4 small elements, while recording the remaining N-K/4 elements to the external file.
The second time, use the heap method to find K/4 small elements from the first filtered N-K/4 elements, while recording the remaining N-K/2 elements to the external file.
。。。
Four, using the heap method to find K/4 small elements from the first filtered N-K/3 elements, this is the K/4 small element even if asked.

To find the median of an unordered array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.