查找与排序 哈希表

哈希表的基本概念

哈希表(Hash table,也叫散列表):
是根据关键码值(Key value)而直接进行访问的数据结构。
也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。

给定表M,存在函数f(key),对任意给定的关键字值key,代入函数后若能得到包含该关键字的记录在表中的地址,则称表M为哈希(Hash)表,函数f(key)为哈希(Hash) 函数。

哈希表的本质上上一个数组(元素是Entry)

Hash Table的查询速度非常的快,几乎是O(1)的时间复杂度
hash就是找到一种数据内容和数据存放地址之间的映射关系
散列法:元素特征转变为数组下标的方法

优点:
不论哈希表中有多少数据,查找、插入、删除(有时包括删除)时间接近常量的时间即0(1)的时间级

缺点:
它是基于数组的,数组创建后难于扩展,某些哈希表被基本填满时,性能下降得非常严重,所以程序员必须要清楚表中将要存储多少数据

特点

1.神奇,实用,粗暴的犯法,空间换时间
2.保证空间足够
3.在构造方法中装入数据,自己可以写代码真假数据。
4.使用(最简单的)除数取余法获得数据存放地址(下标)
5.使用(最简单的)顺移位置法解决冲突。
6.搜索的时间复杂度仅与冲突概率相关,间接地就与装填因子有关,如果空间很多,可以看出时间复杂度为O(1).

代码

代码如下(示例):

/**
 * *************************
 * The second constructor. For Hash code only. It is assumed that paraKeyArray.length <= paraLength.
 * 
 * @param paraKeyArray     The array of the keys.
 * @param paraContentArray The array of contents.
 * @param paraLength       The space for the Hash table.       
 * *************************
 */
	public DataArray(int[] paraKeyArray, String[] paraContentArray, int paraLength) {
		// Step 1.Initialize.
		length = paraLength;
		data = new DataNode[length];

		for (int i = 0; i < length; i++) {
			data[i] = null;
		} // Of for i

		// Step 2.Fill the data.
		int tempPosition;

		for (int i = 0; i < paraKeyArray.length; i++) {
			// Hash.
			tempPosition = paraKeyArray[i] % paraLength;

			// Find an empty position
			while (data[tempPosition] != null) {
				tempPosition = (tempPosition + 1) % paraLength;
				System.out.println("Collision,move forward for key" + paraKeyArray[i]);
			} // Of while

			data[tempPosition] = new DataNode(paraKeyArray[i], paraContentArray[i]);
		} // Of for i
	}// Of the second constructor
	
	/**
	 * *************************
	 * Hash search.
	 * 
	 * @param paraKey The given key.
	 * @return The content of the key.   
	 * ************************
	 */
	public String hashSearch(int paraKey) {
		int tempPosition = paraKey % length;
		while (data[tempPosition] != null) {
			if (data[tempPosition].key == paraKey) {
				return data[tempPosition].content;
			} // Of if
			System.out.println("Not this one for " + paraKey);
			tempPosition = (tempPosition + 1) % length;
		} // Of while

		return "null";
	}// Of hashSearch
	/**
	 * *************************
	 * Test the method.      
	 * *************************
	 */
	public static void hashSearchTest() {
		int[] tempUnsortedKeys = { 16, 33, 38, 69, 57, 95, 86 };
		String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
		DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents, 19);

		System.out.println(tempDataArray);

		System.out.println("Search result of 95 is:" + tempDataArray.hashSearch(95));
		System.out.println("Search result of 38 is:" + tempDataArray.hashSearch(38));
		System.out.println("Search result of 57 is:" + tempDataArray.hashSearch(57));
		System.out.println("Search result of 4 is:" + tempDataArray.hashSearch(4));
	}// Of hashSearchTest
	
	public static void main(String args[]) {
		System.out.println("\r\n-------sequentialSearchTest-------");
		sequentialSearchTest();

		System.out.println("\r\n-------binarySearchTest-------");
		binarySearchTest();

		System.out.println("\r\n-------hashSearchTest-------");
		hashSearchTest();
	}// Of main

运行结果

-------hashSearchTest-------
Collision,move forward for key57
Collision,move forward for key95
Collision,move forward for key95
I am a data array with 19 items.
(38, else)  (57, case)  (95, for)  null null null null null null null (86, while)  null (69, switch)  null (33, then)  null (16, if)  null null 
Not this one for 95
Not this one for 95
Search result of 95 is:for
Search result of 38 is:else
Not this one for 57
Search result of 57 is:case
Search result of 4 is:null

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值