Clolumn1: creacking the oyster

Source: Internet
Author: User

I started reading programming on Saturday and finished reading the first article. But now I write my notes. The first question is actually very simple. But it is impossible for someone who has learned data structures and sorting algorithms to come up with such a method, on the contrary, it is more likely to be thought of by a person who has never learned sorting. The problem is as follows:

Input: a file containing at most n positive integers, each less than n, where n = 107. it is a fatal error if any integer occurs twice in the input. no other data is associated with the integer.

Output: A sorted list in increasing order of the input integers.

Constraints: at most (roughly) a megabyte of storage is available in main memory. ample disk storage is available. the run time can be at most several minutes; a run time of ten seconds need not be dereased.

I did not know how to calculate it at the time, but I suddenly realized it after reading the answer. I have to marvel at the conciseness and elegance of the answer. I feel so stupid. In fact, this problem is very similar to the name registration in the university. When the teacher sees who is there, he will not name the name on the roster. Then, he will draw a check. The order of names is the order of student IDs, or sort order. We don't need to sort the numbers because there are no repeated numbers. We only need to record some numbers. However, there is a problem: a char array needs to be saved in 1 byte, and 107 multiplied by 1 is about 10 MB! It is obvious that the efficiency is too low to exchange data for 10 times. Did you think of it? It uses a bit. one byte can represent eight digits, in this case, the number that can be saved is 1024*1024*8 = 8386608. You can save all the data once or twice. Then, output the result.

The solution to the problem is divided into three steps: first, clear all bits 0, and second, read all the data from the file, and mark the bit [I] with data = 1. The last step is to output all the labeled data in order. It makes sense to name a teacher in a university. There are two points that are difficult to think about. First, bit usage is rarely used now. The second is to use the TAG method without sorting algorithms based on the features of no data duplication up to 107. It is concise and elegant.

Related Keywords:

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.