随笔分类 -  Algorithm

1

CSAPP chapter2 记录(bit_level_coding)
摘要:p_154//5x/8define MSB_BIT (~(~0U >> 1)) int mul5div8(int val){ int sign = (val & MSB_BIT) == MSB_BIT; //取符号位 int bias = (7 + !sign) & 7; int q = val >> 3; int r = val - (q > 3; //向零取整 q = (q > 31; ... 阅读全文

posted @ 2013-07-13 12:33 阿加 阅读(623) 评论(0) 推荐(0)

哈希----字符串----time33
摘要://此处只是获得了字符串的hash值,但是该如何散列到hash表中呢?哪个算法会好些?! 1 //在处理以字符串为键值的哈希时,times33哈希算法有着极快的计算效率和很好的哈希分布 2 3 //小写英文单词 4 #if 1 5 unsigned int str_hash(const char* key) 6 { 7 unsigned int hashval = 0; 8 while(*key) 9 {10 hash = ((hash << 5) + hash) + (unsigned int)*key++;11 }12 13 ret... 阅读全文

posted @ 2013-07-06 19:00 阿加 阅读(623) 评论(0) 推荐(0)

哈希-------开放寻址法-------暴雪哈希
摘要:1 //StringHash.h 2 3 #ifndef __STRINGHASH__ 4 #define __STRINGHASH__ 5 6 #define MAXTABLELEN 1024 //默认哈希索引表大小 7 8 //哈希索引表定义 9 typedef struct _HASHTABLE10 {11 long nHashA; 12 long nHashB; 13 b... 阅读全文

posted @ 2013-07-06 18:17 阿加 阅读(1085) 评论(0) 推荐(0)

最大子数组之和(N)
摘要:1 int maxSum(int *array, int n) 2 { 3 int rvsum = array[0]; 4 int newsum = 0; 5 6 int i = 0; 7 for(i = 0; i = 0)10 newsum += array[i];11 else 12 newsum = array[i];13 14 if(rvsum maxinum)18 maxinum = sum;19 20 s... 阅读全文

posted @ 2013-07-01 16:17 阿加 阅读(239) 评论(0) 推荐(0)

两个散列函数
摘要:除法散列法:散列函数:h(k) = k mod m;关键字为k,m是散列数组的长度m不应是2的幂,当k是一个按基数2^p解释的字符串时,选m=2^p-1可能是一个比较糟糕的选择可以选做m的值常常是与2的整数幂不太接近的质数. 乘法散列法:例子:k = 123456, p = 14, m = 2^14 = 16384,因为当s/(2^32)与(sqrt(5) - 1)/2接近时,s为2654435,故(k*s)&(~0) = 17612864,取其高14位,得散列值67 阅读全文

posted @ 2013-05-16 09:51 阿加 阅读(276) 评论(0) 推荐(0)

无头单链表反转
摘要:1 #include <iostream> 2 3 using namespace std; 4 5 struct node 6 { 7 int data; 8 node *next; 9 }; 10 11 12 node *list_create(void) 13 { 14 node *head = NULL; 15 node **curr... 阅读全文

posted @ 2013-04-29 09:24 阿加 阅读(224) 评论(0) 推荐(0)

数组队列
摘要:使数组中的一个元素始终保留不用,这样当队列“满”时front和rear的值便不相同,可以和队列为空的情况区分开来。通过不允许数组完全填满,问题得以避免。若数组元素共有ARRAY_SIZE个,则有效利用的只有QUEUE_SIZE = ARRAY_SIZE - 1个。当满足下列条件时数组队列为空: (rear + 1) % ARRAY_SIZE == front当满足下列条件时数组队列为“满”: (rear + 2) % ARRAY_SIZE == front数组队列实现如下: queue.h1 /* ... 阅读全文

posted @ 2013-04-15 15:44 阿加 阅读(1046) 评论(0) 推荐(0)

连续整数和
摘要:1 #include 2 3 void show(unsigned int value, unsigned int start, unsigned seq_end) 4 { 5 int i = 0; 6 printf("%d = %d + ", value, start); 7 for(i = 1; i = value)37 break;3... 阅读全文

posted @ 2013-03-23 10:53 阿加 阅读(195) 评论(0) 推荐(0)

汉明码距
摘要:lib/hweight.c 阅读全文

posted @ 2013-03-17 14:12 阿加 阅读(218) 评论(0) 推荐(0)

idr 图示
摘要:看了idr的源码,思想基本同基数树一致,在网上找了几副图:图片引用自:http://blog.csdn.net/yyttiao/article/details/8164029pre_get:单层情况下的布局:在idr.layers == 1 的情况就是上面这图所示,所有的ptr都落在ary对应的地址区域...而对应得到的id值就是0到31.也就是5位..用4字节的低5位表示0到31 这32个地址对应的区域..下面再看下2层的情况从前副图中,我们总结下..在1层中,id最大值为1 << 5(32只需要5个二进制位) * 1 = 在2层中1 << 5(32只需要5个二进制位 阅读全文

posted @ 2013-02-02 18:35 阿加 阅读(286) 评论(0) 推荐(0)

基数树 radix_tree
摘要:Makefile:1 mytest: radix-tree.o test.o 2 gcc -Wall -fno-builtin $^ -o $@3 radix-tree.o: radix-tree.c4 gcc -c $^ -o $@5 test.c: test.o6 gcc -c $^ -o $@7 8 clean:9 rm radix-tree.o test.o myte... 阅读全文

posted @ 2013-02-02 12:29 阿加 阅读(1285) 评论(0) 推荐(0)

大数相乘
摘要:makefile: 1 mytest:test.o multiply.o 2 gcc $^ -o $@ 3 test.o:test.c 4 gcc -I include -c $^ -o $@ 5 multiply.o:multiply.c 6 gcc -I include -c $^ -o $@ 7 8 clean: 9 rm test.o multiply.o mytestinclude/multiply.h://File : multiply.h 1 #ifndef __Jerry_Mul__ 2 #define __Jerry_Mul 3 4 extern int mul... 阅读全文

posted @ 2012-12-14 14:19 阿加

stack栈
摘要:./Makefile: 1 mytest:main.o stack.o 2 gcc $^ -o $@ 3 main.o:main.c 4 gcc -I include -c $^ -o $@ 5 stack.o:stack.c 6 gcc -I include -c $^ -o $@ 7 8 clean: 9 rm main.o stack.o mytest./include/stack.h: 1 #ifndef __Jerry_stack__ 2 #define __Jerry_stack__ 3 4 typedef int stackelem; 5 typedef stackele... 阅读全文

posted @ 2012-12-01 22:30 阿加

洗牌(排列)
摘要:gdb中显示数组内容:p (int [10])*a一: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define NUM 10 6 const int shuffle_initial[NUM] = {1,2,3,4,5,6,7,8,9,10}; 7 int shuffle_flags[NUM] = {0}; 8 int shuffle_final[NUM] = {0}; 9 10 int generate(void) 11 { 12 int i , k , j , l 阅读全文

posted @ 2012-11-29 22:34 阿加 阅读(152) 评论(0) 推荐(0)

32位无符号整数平方根
摘要:1 int isqrt(unsigned x) 2 { 3 unsigned m, y, b; 4 5 m = 0x40000000; 6 y = 0; 7 while(m != 0) 8 { 9 b = y | m; 10 y = y >> 1; 11 if(x >= b) 12 { 13 x = x - b; 14 y = y | m; 15 } 16 m = m >> 2; 17 } 18 return y; 19 } 阅读全文

posted @ 2012-09-20 09:28 阿加 阅读(241) 评论(0) 推荐(0)

交换寄存器中的相应字段
摘要:对于两个寄存器x和y以及掩码m,当第i位的掩码m(i)=1时,交换x和y的第i位内容,而当第i位的掩码m(i)=0时,保留x和y的第i位内容不变x = x ^ y;y = y ^ (x & m);x = x ^ y; 阅读全文

posted @ 2012-09-19 19:42 阿加 阅读(158) 评论(0) 推荐(0)

same number of one's bit
摘要:写了大概一下午,本以为很easy的,写了才知道“边界+细节”,尤其“(ones >> 2) >> ntz”,如果写成了“ones>>(2+ntz)"就会报错,边界部分自己调了。关于bitcount部分,做了修改,基本只用一个大的常数,以及一些小的数字。 4 int bitcount(unsigned int n) 5 { 6 unsigned int tmp; 7 tmp = n & 0x33333333; 8 n = n - tmp; 9 n = (n >> 2) & 0x33333333; 10 tmp = tmp 阅读全文

posted @ 2012-09-18 19:05 阿加 阅读(163) 评论(0) 推荐(0)

求取32位无符号整数中最低位位值为1的位置 && 求取32位无符号整数中最高位位值为1的位置
摘要:3 int bit_pos(unsigned int n) 4 { 5 n = n & (-n); 6 n = n - 1; 7 n = n - ((n>>1)&0x77777777) - ((n>>2)&0x33333333)-((n>>3)&0x11111111); 8 n = (n + (n>>4))&0xf0f0f0f; 9 n = n + ((n >> 8)& 0xf) + ((n >> 16)& 0xf) + ((n >> 24)& 0 阅读全文

posted @ 2012-09-18 12:04 阿加 阅读(1159) 评论(0) 推荐(0)

上下舍入至2的幂次
摘要:4 int clp2(unsigned int n) 5 { 6 --n; 7 n = n | (n >> 1); 8 n = n | (n >> 2); 9 n = n | (n >> 4); 10 n = n | (n >> 8); 11 n = n | (n >> 16); 12 return n + 1; 13 }unsigned flp2 (unsigned x){ x = x | (x >> 1) x = x | (x >> 2) x = x | (x >> 4); x = x | (x 阅读全文

posted @ 2012-09-16 19:57 阿加 阅读(215) 评论(0) 推荐(0)

32位无符号整数中1的个数
摘要:4 int bitcount(unsigned int n) 5 { 6 n = n - ((n>>1)&033333333333) - ((n>>2)&011111111111); 7 n = (n + (n>>3))&030707070707; 8 n = n % 0x3f; 9 return n; 10 } 17 int bitcount(unsigned int n) 18 { 19 n = n - ((n>>1)&0x77777777) - ((n>>2)&0x33333333)-(( 阅读全文

posted @ 2012-09-16 19:52 阿加 阅读(546) 评论(0) 推荐(0)

1

导航