#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. [Angular 2] Value Providers & @Inject

    Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really ...

  2. debian7安装oracle11g

    1,安装必须包 apt-get install gcc g++  make binutils libc6 libc6-dev libstdc++6 libstdc++5 rpm gawk alien ...

  3. A beginner’s guide to Cache synchronization strategies--转载

    原文地址:http://vladmihalcea.com/2015/04/20/a-beginners-guide-to-cache-synchronization-strategies/ Intro ...

  4. centos6安装redis

    1.检查安装依赖程序 yum install gcc-c++ yum install -y tcl yum install wget 2.获取安装文件 wget http://download.red ...

  5. Linux shell 脚本攻略之批量重命名

    摘自:<Linux shell 脚本攻略>

  6. python之装饰器详解

    这几天翻看python语法,看到装饰器这里着实卡了一阵,最初认为也就是个函数指针的用法,但仔细研究后发现,不止这么简单. 首先很多资料将装饰器定义为AOP的范畴,也就是Aspect Oriented ...

  7. proxy代理类

    package cn.hncu.proxy.rent; import java.lang.reflect.InvocationHandler;import java.lang.reflect.Meth ...

  8. [转]Asp.Net MVC 扩展联想控件

    本文转自:http://www.cnblogs.com/bright-lin/archive/2013/02/06/MVC_SuggestBox.html 在web中,为改善用户体验,我们常会将一些文 ...

  9. [改善Java代码]不要随便设置随机种子

    建议30: 不要随便设置随机种子 随机数在太多的地方使用了,比如加密.混淆数据等,我们使用随机数是期望获得一个唯一的.不可仿造的数字,以避免产生相同的业务数据造成混乱.在Java项目中通常是通过Mat ...

  10. [改善Java代码]用偶判断,不用奇判断

    建议21: 用偶判断,不用奇判断 public class Client { public static void main(String[] args) { Scanner in = new Sca ...