【编程珠玑-01章】bitMapSorted

参考1:编程珠玑第一章

参考:2:http://blog.chinaunix.net/uid-26548237-id-3759520.html

以下是位图排序(bitMapSorted)的官方C语言版本

/* Copyright (C) 1999 Lucent Technologies */ 
/* From 'Programming Pearls' by Jon Bentley */ 
 
/* bitsort.c -- bitmap sort from Column 1 
 *   Sort distinct integers in the range [0..N-1] 
 */ 
 
#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("%d\n", i); 
    return 0; 
} 
*****************************************************************************************************************************************************************************************

以下是我写的java版本,并写了注释。

/**  
 * 创建时间:2014年8月9日 下午2:43:44  
 * 项目名称:Test  
 * @author Cao Yanfeng  
 * @since JDK 1.6.0_21  
 * 类说明:  
 */
public class BitMapSortTest {
	private static final int BITSPERWORD=32;
	private static final int SHIFT=5;
	/*0x1F即1*16+15=31,8位二进制形式即 0000 0000 0000 0000 0000 0000 0001 1111*/
	private static final int MASK=0x1F;
	private static final int N=10000000;
	private static int[] array=new int[1+N/BITSPERWORD];

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] data={1000,1,10001,7,99,1000000,0,1001,2,189765,3};
	    printSortedNumbers(data);
		

	}
	public static void printSortedNumbers (int[] data) {
		for (int i = 0; i <N; i++) {
			clr(i);
		}
		for (int i = 0; i < data.length; i++) {
			set(data[i]);
		}
		for (int i = 0; i < N; i++) {
			if (test(i)!=0) {
				System.out.println(i);
			}
		}
	}
	/*array[i>>SHIFT]=array[i>>5]=array[i/2^5]=array[i/32],即获取i在array[]中的第几位
	 * (1<<(i&MASK))=1<<(i&0x1F)=1<<(i&31)=1<<(i%32),即获取i在32个bit中的几位
	 * 比如:i=0,array[0/32]=array[0/32]|(1<<(0%32))
	 * array[0]=array[0]|(1<<(0%32))=array[0]|(1<<0)= 0000 0000 0000 0000 0000 0000 0000 0001;
	 * i=1,array[0]=array[0]|(1<<(1%32))=array[0]|(1<<1)= 0000 0000 0000 0000 0000 0000 0000 0011;
	 * 
	 * */
	public static void set(int i) {
		array[i>>SHIFT]|=(1<<(i&MASK));
	}
	
	public static void clr(int i) {
		array[i>>SHIFT]&=~(1<<(i&MASK));
	}
	public static int test(int i) {
		return array[i>>SHIFT]&(1<<(i&MASK));
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值