通过BitMap进行排序
2012-09-13 16:46
190 查看
//装载于http://blog.csdn.net/v_JULY_v/article/details/6347454
/********************************************
此程序通过位数组对字符进行排序的Demo:
********************************************/
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define BYTESIZE 8 //定义每个Byte中有8个Bit位
void SetBit(char *p, int posi)
{
int i;
for(i = 0; i < (posi / BYTESIZE); i++)
{
p++;
}
*p = *p | (0x01 << (posi % BYTESIZE));//将该Bit位赋值1
return ;
}
void BitMapSortDemo()
{
int i, j;
int num[] = {3,5,2,10,6,12,8,14,9};
//bufferLen这个值是根据带排序的数据中最大值确定
//带排序的最大值是14,因此只需要2个Buytes(16个Bit)就可以了
const int bufferLen = 2;
char *pBuffer = (char *)malloc(bufferLen * sizeof(char));
memset(pBuffer, 0 ,bufferLen);
//将相应Bit位设置为1
for(i = 0; i < 9; i++)
{
SetBit(pBuffer, num[i]);
}
//输出排序结果
for(i = 0; i < bufferLen; i++)
{
for(j = 0; j < BYTESIZE; j++)
{
if((*pBuffer&(0x01<<j)) == (0x01<<j))
{
printf("%d ", i * BYTESIZE + j);
}
}
pBuffer++;
}
printf("\r\n");
return ;
}
int main()
{
BitMapSortDemo();
return 0;
}
/********************************************
此程序通过位数组对字符进行排序的Demo:
********************************************/
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define BYTESIZE 8 //定义每个Byte中有8个Bit位
void SetBit(char *p, int posi)
{
int i;
for(i = 0; i < (posi / BYTESIZE); i++)
{
p++;
}
*p = *p | (0x01 << (posi % BYTESIZE));//将该Bit位赋值1
return ;
}
void BitMapSortDemo()
{
int i, j;
int num[] = {3,5,2,10,6,12,8,14,9};
//bufferLen这个值是根据带排序的数据中最大值确定
//带排序的最大值是14,因此只需要2个Buytes(16个Bit)就可以了
const int bufferLen = 2;
char *pBuffer = (char *)malloc(bufferLen * sizeof(char));
memset(pBuffer, 0 ,bufferLen);
//将相应Bit位设置为1
for(i = 0; i < 9; i++)
{
SetBit(pBuffer, num[i]);
}
//输出排序结果
for(i = 0; i < bufferLen; i++)
{
for(j = 0; j < BYTESIZE; j++)
{
if((*pBuffer&(0x01<<j)) == (0x01<<j))
{
printf("%d ", i * BYTESIZE + j);
}
}
pBuffer++;
}
printf("\r\n");
return ;
}
int main()
{
BitMapSortDemo();
return 0;
}
相关文章推荐
- 应用程序通过对键中的气温进行排序来找出最高气温
- 对于一系列数据通过单击进行排序(可以多次单击)
- 通过CMsvSession获取子条目(Entry)ID时进行过滤和排序
- 通过Comparator和Comparable对集合进行排序
- 通过指针变量用选择法对数组中的10个整数按从大到小进行排序
- 对象数组通过对象的属性进行排序
- java对List<Object>型列表进行排序(通过内部类对compare(**)方法重写)
- 通过Collections对list进行排序、取最大、取最小等操作
- grails通过findBy或findBy查找的结果集进行排序
- 通过实体类中的某一个字段进行排序的两种方式
- 长度为N的数组乱序存放着0带N-1.现在只能进行0与其他数的swap操作,请设计并实现排序,必须通过交换实现排序。
- Java中,通过使用Collections.sort对ArrayList进行排序
- 递归-F-通过归并进行排序
- 数组通过array_push函数进行排序 及 选择排序
- 【PHP】通过键进行降序排序,然后通过键值对字符串连接,最后md5加密算法
- bitmap与桶方式对1000万数据进行排序
- 长度为N的数组乱序存放着0带N-1.现在只能进行0与其他数的swap操作,请设计并实现排序,必须通过交换实现排序。
- C#基础:通过委托给任何对象数组进行排序
- 通过sql语句进行排序(中文、英文都可以排序)
- Java1.7通过collections类的sort方法对ArrayList进行排序无效的解决办法。