10 billion integers, enough memory, how to find the median? Not enough memory, how to find the median?

Source: Internet
Author: User
Enough memory: You can use the idea of a quick sort, which is an O (n), and the algorithm thought is as follows:
• Randomly select an element to place the element smaller than it on the left, and the element larger than it on the right
• If it happens to be in the middle digit position, then it is the median and can be returned directly
• If the number is less than half of it, then the median must be on the left half, recursively to the left.
• Otherwise, the median must be on the right half, based on the number of elements on the left half, the median is the right half, and then recursively to the right half.
Low memory Condition:
Method: Divided method
Idea: An important clue is that these numbers are all integers. Integers have a range, 32-bit system is [ -2^32, 2^32-1], there is a range we can do two points on this range, and then find how many number of ⼩ in mid, more than mid, and then recursion, and based on the quicksort idea of the K-big method similar to

Method Two: The idea of the barrel method: The big to small, divide all the numbers into each cell, map each number to the corresponding interval, count the number of each interval, count the intervals, see where the median grumble is, if it's small enough, use a memory based algorithm, or continue dividing


by one Piece

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.