bitmap排序

/* * Copyright (c) 2011 alexingcool. All Rights Reserved. */ #include <iostream> using namespace std; #define SHIFT 5 #define MASK 0x1F #define N 10000000 #define BITSPERWORD 32 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 array[] = {1, 10, 9, 7, 8, 6, 5, 2, 3, 4, 11, 14, 13, 12, 15, 19, 30, 18, 17, 16}; const int size = sizeof array / sizeof *array; int GetMax(int (&array)[size]) { int maxNum = array[0]; for(int i = 1; i < size; i++) { if(maxNum < array[i]) maxNum = array[i]; } return maxNum; } void main() { int maxNum = GetMax(array); for(int i = 1; i <= maxNum; i++) clr(i); for(int i = 0; i < size; i++) { set(array[i]); } for(int i = 1; i <= maxNum; i++) { if(test(i)) printf("%d ", i); } }

你可能感兴趣的:(bitmap排序)