Java-based bitmap sorting (example of the beginning of the algorithm pearl)

Source: Internet
Author: User

Package COM. SDO. tradinghub. domain; </P> <p> public class bitsorttest {<br/> Private Static final int bitsperword = 32; // integer digits <br/> Private Static final int shift = 5; <br/> Private Static final int mask = 0x1f; // 5-bit masking 0b11111 <br/> Private Static final int n = 10000000; <br/> // simulate a bit array with an int array, total (1 + N/bitsperword) * The bitsperword bit is sufficient to accommodate n <br/> Private Static int [] A = new int [(1 + N/bitsperword)]; </P> <p> P Ublic static void main (string [] ARGs) {<br/> bitsort (New int [] {1,100, 2, 10000,999 9, 4567,789 02 }); <br/>}</P> <p> Public static void bitsort (INT [] array) {<br/> for (INT I = 0; I <N; I ++) <br/> CLR (I); // all bits in the array are 0 <br/> for (INT I = 0; I <array. length; I ++) <br/> set (array [I]); // Phase 2 <br/> for (INT I = 0; I <N; I ++) <br/> If (test (I) <br/> system. out. println (I); <br/>}</P> <p> // set & Amp; mask) bit is 1, that is, the I-bit of the bit array is 1 <br/> Public static void set (int I) {<br/> A [I> shift] | = (1 <(I & Mask )); <br/>}</P> <p> // set the position (I & Mask) of a [I> shift] to 0, that is, the I-bit of the bit array is 0 <br/> Public static void CLR (int I) {<br/> A [I> shift] & = ~ (1 <(I & Mask )); <br/>}</P> <p> // test whether the I-th bit of the bit array is 1 <br/> Public static Boolean test (int I) {<br/> return (A [I> shift] & (1 <(I & Mask) = (1 <(I & Mask )); <br/>}< 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.