#include <stdio.h>
#include <stdlib.h>
#include "chain.c" //include the chain.c to create chain and list
#define NUMBER_SCOPE 69000
#define ARRAY_SIZE 100000000
#define PLUS_RESULT 5 int *createArray(long length){
int i;
int *array=(int*)malloc(length*sizeof(int));
srand((int)time());
printf("Array:");
for(i = ;i<length;i++){
array[i]=rand()%NUMBER_SCOPE - (NUMBER_SCOPE)/;
//printf("%d,",array[i]);
}
return array;
} /*
find the max number from a array
*/
int maxNum(int *array,int length){
int i,max;
max = array[];
for(i = ;i<length;i++){
if(array[i] > max){
max = array[i];
}
}
return max;
}
/*
find the min number from a array
*/
int minNum(int *array,int length){
int i,min;
min = array[];
for(i = ;i<length;i++){
if(array[i] < min){
min = array[i];
}
}
return min;
}
/*
this function to fill barrel ,first parameter is the barrel,second is array
*/
int *fillBarrel(int *barrel,int *array){
int i,site;
for(i = ;i < ARRAY_SIZE;i++){
site = array[i] - barrel[] + ;
barrel[site] = ;
}
return barrel;
}
/*
this function get a barrel array ,return a result chain,
it create by Quick Sort
*/
struct chain *createResult(int *barrel){
int l_site,r_site,l_index,r_index;
struct chain *result;
struct list lst;
l_index = ;
r_index = barrel[] - ;
while(l_index < r_index && ){
if((barrel[l_index] == ) && (barrel[r_index] == )){
l_site = l_index - + barrel[];
r_site = r_index - + barrel[];
// printf("\nl_site = %d\tr_site = %d",l_site,r_site);
// printf("\nbarrel[%d] = %d \nbarrel[%d] = %d",l_index,barrel[l_index],r_index,barrel[r_index]);
if((l_site + r_site) == PLUS_RESULT){
lst.firstNum = l_site;
lst.secondNum = r_site;
/*add in chain*/
result = addlink(result,lst);
l_index ++;
r_index --;
}
else if((l_site + r_site) > PLUS_RESULT){
r_index--;
}
else{
l_index++;
}
}else if(barrel[l_index] != ){
l_index ++;
}
else{
r_index --;
}
}
return result;
}
/*
this function well create a barrel array by the array in the parameters
*/
int *createBarrel(int *array,int length){
int min,max,capacity,*barrel,i;
min = minNum(array,length);
max = maxNum(array,length);
capacity = max - min + ; //+4,because save capacity,min and max.
barrel = (int*)malloc(capacity*sizeof(int));
for(i = ;i<capacity;i++){
barrel[i] = ;
}
barrel[] = capacity; //save barrel capacity in barrel[0]
barrel[] = min; //save min number in barrel[1]
barrel[] = max; //save max number in barrel[2]
return barrel;
} /*
this function could display the information about the barrel and the content
*/
void showBarrel(int *barrel){
int i;
// for(i = 3;i < barrel[0];i++){
// printf("%d\t",barrel[i]);
// }
printf("\narray_length = %d\ncapacity = %d\nmin = %d\nmax = %d\n",ARRAY_SIZE,barrel[],barrel[],barrel[]);
} main(){
int *array,*barrel;
long length;
struct chain *result;
//int a[] = {1,2,3,4,5,7,7,7,7,0};
printf("======main() begin=======\n");
length = ARRAY_SIZE;
array = createArray(length);
//array = a;
//printf("\n====create array over=======");
barrel = createBarrel(array,length);
//printf("\n====create barrel over=======");
barrel = fillBarrel(barrel,array);
//printf("\n====fill barrel over=======\n");
showBarrel(barrel);
//printf("\n====show barrel over=======\n");
result = create();
//printf("\n====create result over=======\n");
result = createResult(barrel);
printf("\n====computer result over=======\n");
/* the function to display the result chain*/
showChain(result);
}

hash桶的更多相关文章

  1. 大厂面试必问题!HashMap 怎样解决hash桶碰撞?

    HashMap冲突解决方法比较考验一个开发者解决问题的能力.下文给出HashMap冲突的解决方法以及原理分析,无论是在面试问答或者实际使用中,应该都会有所帮助.在Java编程语言中,最基本的结构就是两 ...

  2. nginx源码分析之hash的实现

    nginx实现了自己的hash数据结构,正如数据结构中讲述的那样,nginx用开放链表法解决冲突,不过不同的是一旦一个hash表被初始化后就不会被修改,即插入和删除,只进行查询操作,所以nginx通过 ...

  3. php Hash Table(一) Hash Table的结构

    关于Hash Table专题: 一直想深入理解一下php的hash table的实现,以前一直是星星点点的看看,从未彻底的总结过,那就从这个专题开始吧! 主要想总结几个部分:hashtable结构,h ...

  4. Hash中的一些概率计算

    Hash是把锋利的刀子,处理海量数据时经常用到,大家可能经常用hash,但hash的有些特点你是否想过.理解过.我们可以利用我们掌握的概率和期望的知识,来分析Hash中一些有趣的问题,比如: 平均每个 ...

  5. 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数

    ---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...

  6. Mycat 分片规则详解--一致性hash分片

    实现方式:基于hash算法的分片中,算法内部是把记录分片到一种叫做"bucket"(hash桶)的内部算法结构中的,然后hash桶与实际的分片节点一一对应,从此实现了分片.路由的功 ...

  7. Perl中的hash类型

    hash类型 hash类型也称为字典.关联数组.映射(map)等等,其实它们都是同一种东西:键值对.每一个Key对应一个Value. hash会将key/value散列后,按序放进hash桶.散列后的 ...

  8. hash bucket

    什么是bucket bucket的英文解释: Hash table lookup operations are often O(n/m) (where n is the number of objec ...

  9. 曲演杂坛--HASH的一点理解

    HASH,百度百科上做如下定义: Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列 ...

随机推荐

  1. 4K Block Size的Device和 Aligned IO

    http://www.cnblogs.com/cenalulu/p/3587006.html   背景:最近采购了一批新的服务器,底层的存储设备的默认physical sector size从原有的 ...

  2. 通用PE u盘启动盘制作

    导读 通用pe工具箱是现在最老牌的的U盘装系统和维护电脑的专用工具之一,一键式制作.操作简单便捷,几乎100%支持所有U盘,不再为装机烦恼们,抓紧时间下载通用pe工具箱体验下吧. 准备工作 ①从通用p ...

  3. php生成excel或php生成csv

    一.php生成excel 使用phpexcel类文件生成 二.php生成csv <?php$action ="make";if ($action=='make'){ $fp ...

  4. couldn't set tty to ppp discipline invalid argument

    参考: http://pptpclient.sourceforge.net/howto-diagnosis.phtml#conventions http://blog.chinaunix.net/ui ...

  5. Android Service与Activity之间通信的几种方式

    在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activ ...

  6. 《Entity Framework 6 Recipes》中文翻译——第十章EntityFramework存储过程处理(八)

    将插入.更新和删除操作映射到存储过程 问题 您想在存储过程中映射插入.更新和删除操作. 解决方案 下图所示的运动员Athlete实体模型.底层数据库有一张运动员Athlete表.您想使用存储过程进行实 ...

  7. Face The Right Way

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2564   Accepted: 117 ...

  8. 无限滚动 --demo

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  9. Mac OS恢复出厂系统方法

    1.重新启动时按住“Command()”和"R"键盘 2.选择磁盘工具

  10. fancybox的使用

    fancybox,个人没有深入了解,只是为了工作需要,做的一些界面,主要是用的AJAX功能. 首先,需要下载fancybox的js文件以及CSS文件(可能用不到) 其次,在页面中引入 <scri ...