哈希查找
来源:互联网 发布:阿里云 可用区a 编辑:程序博客网 时间:2024/06/10 07:04
哈希查找的关键在于选用哈希函数制作哈希表和冲突处理。
public class hashSearch { public static void main(String[] args) { //创建一个数组 int []array={1,2,3,4,5,6,7,8,9,100,105}; array=createHashTable(array); for(int i=0;i<array.length;i++){ System.out.print(array[i]+" "); } //调用哈希查找找数字100 int index=hash_search(array,106); if(index==-1){ System.out.println("This array does not exist the num"); } else{ System.out.println("index:"+index); } } public static int hash_search(int []array,int num){ int index=-1; int k=num%array.length; int count=0; while(true){ if(array[k]==num){ index=k; break; } k=(k+1)%array.length; count++; if(count>array.length){ break; } } return index; } //制作hashTable,在这里我将数组做成一个环,如果一个数从他的余数的下标开始找直到末尾都未找到位置,则再从头开始找位置 public static int[]createHashTable(int []array){ int []temp=new int[array.length]; //为了防止判断的时候出现余数为0时不能识别是不是我们的元素还是数组初始化元素,所以将temp数组中的设为负值 for(int i=0;i<temp.length;i++){ temp[i]=-1; } for(int i=0;i<array.length;i++){ if(array[i]%array.length!=temp[array[i]%array.length]){ temp[array[i]%array.length]=array[i]; } else{ int a=1; int m=array[i]%array.length; if(a<(array.length-m)){ while((m+a)==(temp[m+a])){ a++; } if(temp[m+a]==-1){ temp[m+a]=array[i]; } } if((m+a)==(array.length-1)&&array[i]!=temp[m+a]){ a=0; while((a)==(temp[a])){ a++; } m=0; temp[m+a]=array[i]; } } } for(int i=0;i<temp.length;i++){ array[i]=temp[i]; } return array; }}
0 0
- 二分查找,分块查找,哈希查找
- 顺序查找,二分查找,哈希查找
- 二分查找、分块查找、哈希查找
- 查找(二):哈希查找和索引查找
- 查找算法之哈希查找
- 查找算法之哈希查找
- 9.6 哈希查找(杂凑查找)
- 查找算法二之哈希查找
- 查找算法之哈希查找
- 查找算法之哈希查找
- 哈希查找(散列查找)
- 哈希查找
- 哈希查找
- 哈希查找
- 哈希查找
- 哈希查找算法
- 哈希查找算法
- 哈希查找
- Hello,World! Python.
- 从零开始学JDBC--1.1 最简单的jdbc连接方式
- Java 方法
- cx_Oracle 中文乱码
- HTC VIVE开发教程(四)
- 哈希查找
- Android 启动adb失败的解决方法
- 操作系统内存管理——分区、页式、段式管理
- Linux 定时任务
- 今日学习——基础程序
- 'keytool' 不是内部或外部命令,也不是可运行的程序
- Integer自动装箱、自动拆箱
- pip安装使用详解
- android 字符串得到分割