Hashing

Hashing

1. Introduction

     Hashing is a technique used for performing insertions, deletions, and finds in constant average time.

Hash function

     Each key is mapped into some number in the range 0  to TableSize -1 and placed in the appropriate cell. And this mapping is called a hash function since there are a finite number of cells and a virtually inexhaustible supply of keys, this is clearly impossible, and thus we seek a hash function that distributes the keys evenly among the cells.

     The only remaining problems :

  • deal with choosing a function
  • deciding what to do when two keys hash to the same value

2. Hash Function

  if the input keys are integers, then simply returning key mod TableSize is generally a reasonable strategy, unless Key happens to have some undesirable properties. In this case, the choice of hash function needs to be carefully considered. For instance, if the table size the is 10 and the keys all end in zero, then the standard hash function is a bad choice. For reasons we shall see later, and to avoid situations like the one above, it is often a good idea to ensure that the table size is prime.

  Usually, the Keys are strings; in this case, the hash function needs to be chosen carefully. The best way use Horner’s rule. If the keys are very long, the hash function will take too long to compute. A common practice in this case is not to use all the characters. Some programmers implement their hash function by using only the characters in the odd spaces, with the idea that the time saved computing the hash function will make up for a slightly less evenly distributed function.

       The main programming detail left is collision resolution. If, when an element is inserted, it hashes to the same value as an already inserted element, then we have a collision and need to resolve it. There are several methods for dealing with this. We will discuss tow of the simplest:  separate chaining and open addressing.

 3. Separate Chaining

  The first strategy, commonly known as separate chaining, is to keep a list of all elements that hash to the same value.

  • To perform a search, we use the hash function to determine which list to traverse. we then search the appropriate list.
  • To perform a insert, we check the appropriate list to see whether the element is already in place. If the element turns out to be new, it can be inserted at the front of the list, since it is convenient and also because frequently it happens that recently inserted elements are the most likely to be accessed in the near future.

4. Hash Tables without Linked Lists

  Separate chaining hashing has the disadvantage of using linked lists. This could slow the algorithm down a bit because of the time required to allocate new cells, and also essentially requires the implementation of a second data structure.

  There are three common collision resolution strategies alternatively:

  • Linear Probing
  • Quadratic Probing
  • Double Hashing

5. Rehashing

  If the table gets too full, the running time for the operations will start taking too long and insertions might fail for open addressing hashing with quadratic resolution. This can happen if there are too many removals intermixed with insertions. A solution, then, is to build another talbe that is about twice as big and scan down the entire original hash table, computing the new hash value for each element and inserting it in the new table.

  This entire operation is called rehashing.

Rehashing can be implemented in several ways with quadratic probing:

  • rehash as soon as the table is half full
  • rehash only when an insertion fails
  • rehash when the table reaches a certain load factor, Since performance does degrade as the load factor increase, the third strategy, implemented with a good cutoff, could be best.

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110273.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 剑指 Offer:Redis 的 14 大使用场景!

    剑指 Offer:Redis 的 14 大使用场景!

    2022年2月11日
  • 变量

    变量

    2022年3月3日
  • Tomcat学习—Tomcat的server.xml配置文件

    今天开始学习Tomcat的配置文件,自己学习和上网查看整理server.xml 的笔记!

    2022年2月24日
  • Redis入门_redis菜鸟教程

    Redis入门_redis菜鸟教程Redis文章目录Redis一、Redis概述二、Redis应用场景三、Redis的下载和安装四、Redis服务的启动五、Redis命令行工具六、Redis基础知识七、Redis数据类型1.key(键)2.String(字符串)3.List(列表)4.Set(集合)5.Zset(有序集合)6.Hash(哈希)八、实践案例九、Redis常用查询指令十、Jedis的使用十一、Redis可视化工具十二、持久化概念十三、RDB十四、AOF十五、AOF重写十六、RDB与AOF的选择一、Re

    2022年9月14日
  • 小试 ScriptManager

    小试 ScriptManager1概述ScriptManager控件管理用于MicrosoftASP.NETAJAX页面的客户端脚本。默认情况下,ScriptManager控件将MicrosoftAJAX库的脚本与页面注册到一起,这使脚本可以使用类型系统扩展并支持局部页面输出和Web服务调用。在页面中,必须使用ScriptManager控件来使下列MicrosoftASP.NETAJAX…

    2022年7月13日
  • java socket 实现原理_java socket原理

    java socket 实现原理_java socket原理TCP/IP模型中有5层结构:应用层、传输层、网络层、数据链路层以及物理层。其中IP协议是位于网络层的,TCP协议是位于传输层的,通过IP协议可以使两台计算机使用同一种语言,从而允许Internet上连接不同类型的计算机和不同的操作系统的网络,IP协议只能保证计算机能够接收和发送分组数据,当计算机要和远程的计算机建立连接时,TCP协议会让它们建立连接:用于发送和接收数据的虚拟电路。在Java中…

    2022年7月7日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号