Bitmap sorting-repeated Elements

Source: Internet
Author: User

The previously discussed bitmap sorting has a condition that the sorted integers do not have repeated items. According to the "programming Pearl", a bitmap sorting rule that allows repeated items is implemented, but this sorting still has a limit: the maximum number of repeated entries is allowed.

Problem description:

Given the input file, each record in the file is an integer (each data can be repeated for up to 10 times), and each record can be at most n, n <= 10000000, sort all records in the file (from small to large), and then input them to the specified file.

Analysis:

Since each data item can be repeated for up to 10 times, it will not work if one bit is used to represent an integer, and at least four bit is required. Therefore, each INTEGER (assuming 32 bits) can represent eight data items. To achieve this, you also need to find the subscript label of each data item in the array and the "bit" offset POS of the corresponding subscript data.

Set_add is used to add the number of occurrences of a data item. The implementation method is as follows: first, four bits indicating the current data item are found based on the label and POS, then convert the four bits into an integer TMP (number of occurrences) to make TMP ++, finally, write TMP back to the bit of the current data item (first clear the original bit and then overwrite it ).

Clear the bit corresponding to the current data item.

The number of times the current data item appears.

The C ++ implementation is as follows:

1 # include <iostream> <br/> 2 # include <fstream> <br/> 3 using namespace STD; <br/> 4 const int max = 40000; <br/> 5 const int bitsperword = 32; <br/> 6 const int num = 4 * max/bitsperword; <br/> 7 int A [num + 1]; <br/> 8 <br/> 9 void set_add (int I) <br/> 10 {<br/> 11 int label = I> 3; <br/> 12 INT Pos = I * 4% 32; <br/> 13 <br/> 14 int TMP = (a [label]> POS) & 0x0f; <br/> 15 TMP ++; <br/> 16 A [label] & = ~ (0x0f <POS); <br/> 17 A [label] + = TMP <Pos; <br/> 18} <br/> 19 void clear (int I) <br/> 20 {<br/> 21 int label = I> 3; <br/> 22 int Pos = I * 4% 32; <br/> 23 <br/> 24 A [label] & = ~ (0x0f <POS); <br/> 25} <br/> 26 int test (int I) <br/> 27 {<br/> 28 int label = I> 3; <br/> 29 int Pos = I * 4% 32; <br/> 30 int TMP = (a [label]> POS) & 0x0f; <br/> 31 return TMP; <br/> 32} <br/> 33 int main () <br/> 34 {<br/> 35 ifstream fin ("source. file "); <br/> 36 ofstream fout (" sort. file "); <br/> 37 int TMP; <br/> 38 int I = 0; <br/> 39 for (I = 1; I <MAX + 1; I ++) <br/> 40 clear (I); <br/> 41 while (Fin> TMP) <br/> 42 {<br/> 43 set_add (TMP); <br/> 44} <br/> 45 for (I = 1; I <MAX + 1; I ++) <br/> 46 {<br/> 47 TMP = test (I); <br/> 48 cout <TMP <Endl; <br/> 49 for (Int J = 1; j <= TMP; j ++) <br/> 50 fout <I <Endl; <br/> 51} <br/> 52} <br/>

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.