一.查找概述
二.代码展示
三.总结
一.查找概述
在计算机中,存在着大量信息,对于如何查找特定的元素是一个很重要的问题,查找的效率也是一个很重要的因素,因此产生了不同的查找算法。常见的有顺序查找,二分查找,插值,哈希等等。
平均查找长度(Average Search Length,ASL):需和指定key进行比较的关键字的个数的期望值,称为查找算法在查找成功时的平均查找长度。
对于含有n个数据元素的查找表,查找成功的平均查找长度为:ASL = P i*C i的和。
P i:查找表中第i个数据元素的概率。
C i:找到第i个数据元素时已经比较过的次数。
顺序查找
说明:顺序查找适合于存储结构为顺序存储或链接存储的线性表。
基本思想:顺序查找也称为线形查找,属于无序查找算法。从数据结构线形表的一端开始,顺序扫描,依次将扫描到的结点关键字与给定值k相比较,若相等则表示查找成功;若扫描结束仍没有找到关键字等于k的结点,表示查找失败。
复杂度分析:
查找成功时的平均查找长度为:(假设每个数据元素的概率相等) ASL = 1/n(1+2+3+…+n) = (n+1)/2
当查找不成功时,需要n+1次比较,时间复杂度为O(n);
所以, 顺序查找的时间复杂度为O(n)。
二分查找
二分查找要求元素必须是有序的,如果无序首先必须进行排序。
基本思想:也称为是折半查找,属于有序查找算法。用给定值k先与中间结点的关键字比较,中间结点把线形表分成两个子表,若相等则查找成功;若不相等,再根据k与该中间结点关键字的比较结果确定下一步查找哪个子表,这样递归进行,直到查找到或查找结束发现表中没有这样的结点。
哈希表
散列表(Hash table,也叫哈希表),是根据键(Key)而直接访问在内存存储位置的数据结构。也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这加快了查找速度。这个映射函数称做散列函数,存放记录的数组称做散列表。
在哈希化以后有几个元素的下标值相同,这就叫做 冲突。
解决冲突的方法 1:拉链法 2.开放地址法
拉链法就是在发生冲突的数组位置产生链表,拿java集合类中的HashMap来说吧,如果这里的链表长度大于等于8的话,链表就会转换成树结构,当然如果长度小于等于6的话,就会还原链表。以此来解决链表过长导致的性能问题。
开发地址法指:通过哈希函数得到的下标数组有元素,那么我们就重新在找一个新的地址。常见的方法有线性探测,二次探测,再哈希化。
这里我们采用的是取余获得下标位置和瞬移位置法来解决冲突。
二.代码实现
package dataStructure.serach;
/**
* @author Donghao Xu
*/
public class DataArray {
/**
* an inner class for DataArray.
* Use a key-value to store .
*/
class DataNode {
/**
* The key.
*/
int key;
/**
* The content.
*/
String content;
/**
* the first constructor
*/
public DataNode(int paraKey, String paraContent) {
key = paraKey;
content = paraContent;
}//
public String toString() {
return "( " + key + ", " + content + ")";
}
}//of class DataNode
/**
* the data aray.
*/
DataNode[] data;
int length;
/**
* the second constructor
*/
public DataArray(int[] paraKey, String[] paraContent) {
length = paraKey.length;
data = new DataNode[length];
for (int i = 0; i < length; i++) {
data[i] = new DataNode(paraKey[i], paraContent[i]);
}//of for i
}//of the second constructor
public String toString() {
String resultString = "";
for (int i = 0; i < length; i++) {
resultString += data[i] + " ";
}//of for i
return resultString;
}//of toString
/**
* sequential search.
*
* @param paraKey the given key.
* @return the content of the key.
*/
public String sequentialSearch(int paraKey) {
data[0].key = paraKey;
int i;
for (i = length - 1; data[i].key != paraKey; i--) ;
return data[i].content;
}//of sequentialSearch
/**
* test
*/
public static void sequentialSearchTest() {
int[] tempUnsortedKeys = {-1, 5, 6, 7, -2, 8, -3};
String[] tempContents = {"null", "switch", "a", "b", "c", "d", "e"};
DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);
System.out.println(tempDataArray);
System.out.println("Search result of 6 is :" + tempDataArray.sequentialSearch(6));
}//of sequentialSearchTest
/**
* Binary Search It is assumed that the keys are sorted in ascending order.
*
* @param paraKey the given key.
* @return the content of the key.
*/
public String binarySearch(int paraKey) {
int tempLeft = 0;
int tempRight = length - 1;
int tempMiddle = (tempLeft + tempRight) / 2;
while (tempLeft <= tempRight) {
tempMiddle = (tempLeft + tempRight) / 2;
if (data[tempMiddle].key == paraKey) {
return data[tempMiddle].content;
} else if (data[tempMiddle].key <= paraKey) {
tempLeft = tempMiddle + 1;
} else {
tempRight = tempMiddle - 1;
}
} // Of while
// Not found.
return "null";
}// Of binarySearch
/**
* test
*/
public static void binarySearchTest() {
int[] tempSortedKeys = {1, 3, 5, 6, 7, 9, 10};
String[] tempContents = {"if", "then", "else", "switch", "case", "for", "while"};
DataArray tempDataArray = new DataArray(tempSortedKeys, tempContents);
System.out.println(tempDataArray);
System.out.println("Search result of 10 is: " + tempDataArray.binarySearch(10));
System.out.println("Search result of 5 is: " + tempDataArray.binarySearch(5));
System.out.println("Search result of 4 is: " + tempDataArray.binarySearch(4));
}//of binarySearchTest
/**
* the entrance of the program.
*
* @param args not used now.
*/
public static void main(String[] args) {
System.out.println("\r\n-------sequentialSearchTest-------");
sequentialSearchTest();
System.out.println("\r\n-------binarySearchTest-------");
binarySearchTest();
}//of main
}//of class DataArray
三.总结
对于哈希表,自己也停留在会用的层面,在Java中HashMap的部分源码和解决冲突的过程又重新看了一遍之前学习Java的笔记。哈希函数是一个很重要的部分,设计的好的话就能减少冲突的发生,在Java中,采用了数组,链表,红黑树进行存储,使用初始容量,负载因子两个参数来进行扩容的判断。可见高级语言在哈希表方面的设计属实太全面了。.哈希能够快速地进行 插入修改元素, 删除元素, 查找元素 等操作,哈希表中的元素可以直接查找,因此时间复杂度为O(1),但是哈希又是用空间换取了时间的效率。