How to use 1M of memory to sort 1 million 8-bit numbers

Source: Internet
Author: User

See this article today, quite shocked, exclamation algorithm of the "Avatar." The right algorithm can be used to accomplish seemingly impossible things.

The earliest issue was given on the stack Overflow website (sorting numbers in RAM):

Topic:

Provides a 1M ROM and 1M of RAM, an input stream and an output stream. Program code eventually burned in 1M ROM, the program can use 1M of RAM to operate. The input stream, in turn, enters 1 million 8-bit integers that require the output stream to output these 1 million-digit sorted results.

has been able to search a lot of solutions, today to see a foreign programmer's analysis, it is very interesting, want to turn his analysis process simple here. Simply look, it is impossible, 1 million 8 digits in any case can not be installed in 1M of memory. The sorting process is more efficient by using merge sorting. The hardest part is how to save 1 million of the sorted numbers. Another way of thinking, do not have to save every number itself, because the numbers are already sorted, then the difference between the adjacent two numbers is very small, if in extreme cases, the difference between the two numbers is very large, then there is bound to be more of the number of adjacent numbers between the small difference between the values, Therefore, the space required to store all the 1 million-digit differences can be estimated.

The average size of each difference is: 10^8/100 = 100,100 requires 7 bit to represent, so there is a total of 1 million X7 = 875, 000 bytes, less than 1M of space, but there are many values greater than 128 need to encode, so that some values are encoded more than 7 bit. Therefore, the next question is how to encode the 1 million difference, can compress the space as far as possible, the author cited the arithmetic code to solve this problem, choose a simple coding rule, namely: look at the first bit bit, if it is 0, then 6 bit bit represents the value, if it is 1, then the difference is 64, Continue reading after a bit, if still 1, then the difference continues to accumulate 1, until read 0, and then read the following 6 bit, so that can represent all possible values, so down, the final calculation of the required memory is 1070312.5bytes, still greater than 1M.

Finally, a Huffman coding solution for the problem was adopted. Arithmetic code to understand, Huffman coding also seems to understand, but how to use to solve this problem, is not too clear. At the same time, the author also gives 339 lines of practical code to solve the problem.

Copyright NOTICE: Welcome reprint, Hope in your reprint at the same time, add the original address, thank you with

How to use 1M of memory to sort 1 million 8-bit numbers

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.