#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. hibernate 实体关系映射笔记

    @经常使用属性说明:     @Entity:实体类     @Table:指定相应数据表     @Id:主键,使用能够为null值的类型,假设实体类没有保存到数据库是一个暂时状态     @Col ...

  2. onClick,onServerClick,onClientClick

    <asp:button id=button1 runat=server test=button1 onclick=button1_onclick/> <input type=butt ...

  3. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  4. Linux内核--网络栈实现分析(一)--网络栈初始化--转

    转载地址 http://blog.csdn.net/yming0221/article/details/7488828 作者:闫明 本文分析基于内核Linux Kernel 1.2.13 以后的系列博 ...

  5. grep时排除指定的文件和目录

    参考:http://winterth.duapp.com/notes/ar03s04.htmlhttp://blog.sina.com.cn/s/blog_7169c8ce0100qkyf.html ...

  6. java中关于json传图片的方法

    一般来说传图片可以以流的形式来传输,即便是用json传输,一般也都是传一个地址,而图片都存在服务器上,然后顺着地址发送请求下载图片. 但是这次公司的项目中,图片是存在oracle数据库中的blob字段 ...

  7. JavaWeb中登陆功能

    首先我们要JavaWeb登陆的基本流程:JSP页面发送请求-->Servlet-->Servlet通过调用方法从数据库中得到数据并将结果返回页面 我们先建立三个jsp页面,包括login. ...

  8. The method load(Class, Serializable) in the type HibernateTemplate is not applicable for the arguments (Class, int)

    引入别人的项目发现利用HibernateTemplate的load的方法报错了.错误提示为: The method load(Class, Serializable) in the type Hibe ...

  9. java.lang.ClassNotFoundException: com.servlet.HandlesearchclassesServlet

    错误的原因: 原来命名为Handlesearchclasses导致与系统预留字冲突, 导致servlet找不到,出现404错误, 因此该变类名和web.xml里面的配置文件即可.

  10. 标签切换JS代码

    //标签切换 var nav = $('.index-nav'); var content = $('.index-nav-content li'); function hoverNav ($eleA ...