随笔分类 -  算法实现

学习各种算法,动手实现和分析总结。
摘要:import java.util.ArrayList;import java.util.HashMap;import java.util.regex.Matcher;import java.util.regex.Pattern;import lombok.AllArgsConstructor;imp... 阅读全文
posted @ 2015-05-15 11:16 samu 阅读(220) 评论(0) 推荐(0)
摘要:/* Convert a UTF-8 string into a UCS-2 array. */void tcstrutftoucs(const char *str, uint16_t *ary, int *np){ assert(str && ary && np); const unsigne... 阅读全文
posted @ 2014-07-15 22:12 samu 阅读(286) 评论(0) 推荐(0)
摘要:在arena_s结构中,由NBINS数组将bin按照不同规模等级分别存储,每一个等级对应一颗run树,即一颗以chunk_map_t为节点的红黑树,而这些chunk_map_t节点实际分布于各个chunk的chunk_map_t数组中(每个chunk缺省为4M大小),由于chunk其实地址是chun... 阅读全文
posted @ 2014-04-23 23:45 samu 阅读(574) 评论(0) 推荐(0)
摘要:在调用arena_malloc_small过程中,要根据申请内存大小,进行对齐计算,然后分配一个整块儿。算法如下:1)定义一个SIZE_CLASSES宏,它主要用于生成后面两个表,small_size2bin与arena_bin_info数组;2)根据small_size2bin查找当前申请内存块大... 阅读全文
posted @ 2014-04-20 17:09 samu 阅读(776) 评论(0) 推荐(0)
摘要:一、5种malloc方法1)tcache_alloc_small2)arena_malloc_small3)tcache_alloc_large4)arena_malloc_large5)huge_malloc//written in jemalloc_internal.h fileimalloct... 阅读全文
posted @ 2014-04-19 23:03 samu 阅读(2345) 评论(0) 推荐(0)
摘要:void swap(void *v[], int a, int b){ void* tmp; tmp = v[a]; v[a]=v[b]; v[b]=tmp;}/* qsort: sort v[left]...v[right] into increasing order */void qsort(void *v[], int left, int right, int (*comp)(void *, void *)){ int i, last; if (left >= right) /* do nothing if array contains */ retur... 阅读全文
posted @ 2014-03-25 18:57 samu 阅读(249) 评论(0) 推荐(0)
摘要:#include int thread_flag;pthread_cond_t thread_flag_cv;pthread_mutex_t thread_flag_mutex;void initialize_flag (){/* Initialize the mutex and condition variable.pthread_mutex_init (&thread_flag_mutex, NULL);pthread_cond_init (&thread_flag_cv, NULL);/* Initialize the flag value. */thread_flag 阅读全文
posted @ 2014-03-20 20:10 samu 阅读(193) 评论(0) 推荐(0)
摘要:#include #include #include struct job {/* Link field for linked list.struct job* next;*//* Other fields describing work to be done... */};/* A linked list of pending jobs.struct job* job_queue;*//* A mutex protecting job_queue. */pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;/* A semap 阅读全文
posted @ 2014-03-20 20:07 samu 阅读(184) 评论(0) 推荐(0)
摘要:约瑟夫环问题: 输入:1)总共人数;2)每次被杀的序号数; 输出:最后一个活着的序号python代码如下:n=int (input('please input the number of people:') )k=int (input('please input the discard number:'))a=[]for i in range(n): a.append(i+1)print 'all the serial number of people:'print ai=0j=1while len(a)>1: if j==k: d... 阅读全文
posted @ 2014-03-19 18:35 samu 阅读(182) 评论(0) 推荐(0)
摘要:普通表达式一般由数字、变量与+-*/()运算符号组成。例如:(a+b)*3 - (a-b)/2其逆波兰表达式是:a b + 3 * a b - 2 / -本质上,普通表达式是中序表达式,逆波兰表达式是后序表达式。一)、由普通表达式生成逆波兰表达式 1、初始化1个逆波兰字符串变量outstr,以及1个符号栈;自左向右输入表达式串; 2、如果当前字符为数值或变量,则直接添加到逆波兰字符串后outstr; 3、如果当前字符为运算符号,如果是“(”,则直接压入符号栈;如果是“)”,则从栈中逐项pop运算符号,追加在outstr后;如果是其他运算符号,比较当前与栈顶的运算符优先级,如果当前低,则pop栈 阅读全文
posted @ 2014-03-18 13:12 samu 阅读(1731) 评论(0) 推荐(1)
摘要://8-bucket sortvoid bucket_sort( int *arr, int n ){ vector > buckets; int i; for( i=0; i tmps; buckets.push_back( tmps ); } int base; int *backup = new int[n]; int *pos = new int[n]; for( i=0; i<n; backup[i]=arr[i], pos[i]=i, ++... 阅读全文
posted @ 2014-03-18 12:54 samu 阅读(137) 评论(0) 推荐(0)
摘要:堆排序//7-heap sort methodvoid build_heap( int *arr, int start, int n ){ if( (start+1)*2 > n ) return; if( (start+1)*2 == n ) { if( arr[start] 1; offset-=2 ) { if( arr[offset/2-1] 1; ) { --offset; ... 阅读全文
posted @ 2014-03-18 12:53 samu 阅读(156) 评论(0) 推荐(0)
摘要:#includeusing namespace std;//1-insert sort methodvoid insert_sort( int *arr, int n ){ int i, k; for( i=1; i0; --k ) { if( arr[k] 0; --k ) { for( i=0; i arr[i+1] ) { int tm... 阅读全文
posted @ 2014-03-10 21:29 samu 阅读(203) 评论(0) 推荐(0)
摘要:char* urlencode(const void* buf, size_t size) { _assert_(buf && size = 'A' && c = 'a' && c = '0' && c > 4; if (num = ep) break; c = *str; if (c >= '0' && c = 'a' && c = 'A' && c = ep) break; 阅读全文
posted @ 2014-03-01 15:41 samu 阅读(1950) 评论(0) 推荐(0)
摘要:一个数据矩阵,选定某个位置的数据元作为参考,寻找与该数据元值相同的连成一片的最大范围。如下二维数组中所示,选择arr[1][3]作为参考对象,从位置(1,3)开始寻找所有相连的值为‘1’的元素。 阅读全文
posted @ 2014-02-28 19:28 samu 阅读(657) 评论(1) 推荐(0)
摘要:// QuickSort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"int partition( int*p, int l, int r ){ int val,ml, mr, tmp; val = p[l]; ml=l+1; mr=r; /// while(1) { for( ; ml val ) break; } for( ; mr>ml; mr-- ) { if( p[mr] val ) ... 阅读全文
posted @ 2014-02-26 14:15 samu 阅读(211) 评论(0) 推荐(0)
摘要:MD5与SHA算法一样,利用他们可以计算某段数据的唯一hash值,常用做校验码。而MD5比SHA算法性能高。在我参加的一个项目中,主要用MD5码值来去重,因此对计算性能要求较高。网上有对MD5算法并行化方法,能保证计算结果与线性计算法一样。由于我只需要唯一标记,并不要求它与线性MD5计算结果一样,所以,我通过分片多线程计算的方法实现提速,也充分利用了多核CPU的并行计算优势。依据:B = (b1,b2,b3,b4...bn)--->(m1,m2,m3...mn)----+--->M---->(md5) result = 16 bytesC#代码:class SafeQueue 阅读全文
posted @ 2014-01-11 00:06 samu 阅读(1695) 评论(0) 推荐(0)