Showing posts with label Bit Sort. Show all posts
Showing posts with label Bit Sort. Show all posts

Bit Sort Program in Java

How to write bit sort program in Java?

#include <stdio.h>
#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 10000000
int a[1 + N/BITSPERWORD];

void set(int i) {
    a[i>>SHIFT] |=  (1<<(i & MASK));
}

void clr(int i) {
    a[i>>SHIFT] &= ~(1<<(i & MASK));
}
int  test(int i){
    return a[i>>SHIFT] &   (1<<(i & MASK));
}

int main()
{   int i;
    for (i = 0; i < N; i++)
        clr(i);
/*  Replace above 2 lines with below 3 for word-parallel init
    int top = 1 + N/BITSPERWORD;
    for (i = 0; i < top; i++)
        a[i] = 0;
 */
    while (scanf("%d", &i) != EOF)
        set(i);
    for (i = 0; i < N; i++)
        if (test(i))
            printf("%dn", i);
    return 0;
}

int a[1 + N/BITSPERWORD]; // allocates BitSet of N size