使用位图解决电话号码排序问题--编程珠玑学习笔记 第一章
2008-01-15 11:03
330 查看
本文来源于《编程珠玑 第二版》第一章
位图和位向量表示:
用20位表示集合{2,3,5,6,10,13,19}为:01101100010010000010
电话号码排序问题:
输入:一个文件,有n个整数,没个整数要小于n,无重复
输出:以升序排序文件
约束:可用内存1MB,时限10s
算法:
//phase 1: initialize set to empty
for i=[0,n)
bit[i]=0;
//phase 2: insert present elements into the set
for each i in the input file
bit[i]=1
//phase 3: write sorted output
for i=[0,n)
if bit[i]==1
write i on the output file
自带源代码如下:
/* 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;
}
位图和位向量表示:
用20位表示集合{2,3,5,6,10,13,19}为:01101100010010000010
电话号码排序问题:
输入:一个文件,有n个整数,没个整数要小于n,无重复
输出:以升序排序文件
约束:可用内存1MB,时限10s
算法:
//phase 1: initialize set to empty
for i=[0,n)
bit[i]=0;
//phase 2: insert present elements into the set
for each i in the input file
bit[i]=1
//phase 3: write sorted output
for i=[0,n)
if bit[i]==1
write i on the output file
自带源代码如下:
/* 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;
}
相关文章推荐
- 使用内置函数解决PHP多维数组排序问题
- 解决使用HIBERNATE对ORACLE数据库中数据进行排序分页错乱的问题
- oracle使用小技巧--解决按号单排序和横向记录拼字段问题
- 【编程珠玑-读书笔记】用位图解决排序问题--仔细分析问题的重要性
- 使用IntentService解决点我达骑手APP消息提醒机制中的优先级排序问题
- 使用POI是读取excel文件中电话号码及日期出现的问题及解决
- 解决lucene 1.* 使用排序后内存溢出问题
- 使用qsort对二维字符数组排序疑难问题调试及解决过程
- 电话号码排序问题——位图排序
- oracle使用rownum跟排序的冲突问题解决
- jstree详解(解决jstree树的sort和move_node和drop drag一起使用时不能排序的问题)(附源码)
- 使用RDD解决spark TopN问题:分组、排序、取TopN
- Java使用Comparable解决排序问题
- 编程珠玑第一章,电话号码排序问题
- 使用dataframe解决spark TopN问题:分组、排序、取TopN和join相关问题
- 使用比特位图解决八皇后问题
- bitmap位图方案解决海量数据文件排序的问题
- 解决 android.support.v7.widget.GridLayout 使用 xmlns:app 出现 error 的问题
- Oracle 使用ROWNUM分页时的排序问题
- 解决使用cmd或powershell进行数据库(MySQL,SQLite3...)查询时中文乱码的问题