C + + Implementation Bitmap Sort instance __c++

Source: Internet
Author: User

Turn from: http://www.jb51.net/article/53703.htm


In the book "Programming Zhuji", we mention a bitmap sorting method that is not mentioned in the introduction of algorithms, which is to pursue time efficiency (linear time) by sacrificing space efficiency to achieve the goal of time-space compromise and mutual win. In this paper, a simple example of the form of bitmap ordering idea.

I. Description of the problem

1. Input: A file that contains up to 10 million nonnegative integers

2. Features: ① each number is a nonnegative integer less than 10000000, ② no duplicate numbers, and no association between ③ data.

3. Constraint: ① up to 1MB of memory space available, ② disk space sufficient, ③ run time up to a few minutes, preferably linear time.

4. Output: An integer sequence sorted in ascending order.

second, the idea of bitmap ordering

Because of the number of data to be sorted, we simply use common sorting methods with less time efficiency and a long running time. And the memory space is limited (limited to around 1MB), so we can't read all integers into memory at the same time (if each integer is stored in 7 bytes, then 1MB memory space can only save about 143,000 digits). Of course we can read the input file multiple times and sort it multiple times, but a better solution is to use a bitmap sort, with a limited 1MB memory space and a single trip.

1. According to the largest number of the set to be sorted, open up a bit array to represent the integers to be sorted in the set;

2. The corresponding position of the number in the array in the sorted set is 1, and the other is placed 0;

For example, the collection {1,2,3,5,8,13} to be sorted can be represented as: 0-1-1-1-0-1-0-0-1-0-0-0-0-1

This sort process can naturally be divided into three steps:

The first step: all the bits are placed to 0;

Step two: By reading into each integer in the file, each corresponding bit is placed to 1;

Step three: Check each digit, if the bit is 1, output the corresponding integer.

Note: Bitmap sorting uses a bits instead of an integer to represent 0 or 1, which can greatly reduce the amount of memory space needed. The premise of using bitmap sorting is to know the maximum number in the sorted sequence. The disadvantage of bitmap sorting is that some numbers do not appear, and still have one bit reserved for them. Therefore, bitmap sorting is more suitable for keyword-intensive sequences, such as a city's telephone number.

Pseudo code is as follows:?

1 2 3 4 5 6 7 8 9 10 /*phase 1:initialize set to empty*/for i = [0, n) bit[i] = 0/*phase 2:insert present elements into the set*/        For all I in the input file bit[i] = 1/*phase 3:write sorted output*/for i = [0, N) if bit[i] = 1 Write I on the output file

Performance: Time complexity can be up to O (n), 1MB contains 8*1024*1024 bit, the required memory 10000000/(8*1024*1024) =1.20MB, if not strictly limited words can be regarded as basic compliance requirements.

Three, bitmap sorting implementation

Bitmap sorting, we need to consider: give a number, how to find the location of its corresponding bitmap, the method is to first find the number of the corresponding byte, and then find the number corresponding to the bit. For example:?

1 2 unsigned char bitmap[2]; /* can represent 16 number, namely 0~15 * *

A byte has eight digits, 5 represents the 5th bit of the No. 0 byte, and 14 represents the 1th byte of the 6th bit.

In order to simplify bit processing here, we use the C + + standard library

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.