文章目录
静态查找:
- 分为顺序查找和折半查找。(或线性查找和二分查找)。
- 一个数据项或记录的数据结构:
typedef struct {
KeyType key; // 关键字域
... // 其他域
}DataItem;
一、顺序查找
(一)、定义
- Sequential Search,函数命名:sequentialSearch;
- 顺序查找:可用在顺序储存结构 ( 顺序表 ) 和线性链表结构 ( 链表 ) 中。
- 顺序查找表的0号单元留空 : 性能好一倍。
(二)、原理
- 从表中最后一个数据项开始,从后往前。
- 比较表中数据项的关键字与给定的关键字。
- 若到第一个数据项都匹配不成功,则查找失败。
(三)、算法
- 把给定的 key 赋值给 0号单元。——“哨兵”
- 从最后一个往前。
- 找不到时,返回0(0号单元位置)。
(四)、代码
/**
* 顺序查找
*
* @param key
* @return key相对应的值
*/
public String sequentialSearch(int key) {
searchTable[0].key = key; // "哨兵"
int i;
for (i = length - 1; searchTable[i].key != key; i--)
;
return searchTable[i].value;
}
二、折半查找
(一)、定义
- Binary Search,函数命名:binarySearch;
- 三个位置:low, mid, high
- 具体原理看代码。
(二)、代码
/**
* 折半查找
*
* @param key
* @return
*/
public String binarySearch(int key) {
int low = 1;
int high = length;
int mid;
while(low < high) {
mid = (low + high) / 2;
if(searchTable[mid].key == key) {
return searchTable[mid].value;
} else if(searchTable[mid].key < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return null;
}
三、输出样例
(一)、源码
package search;
public class Search {
/** 查找表 */
DataItem[] searchTable;
/** 表长度 */
int length;
class DataItem {
/** 关键字域 */
int key;
/** 其他域 */
String value;
DataItem(int key, String value) {
this.key = key;
this.value = value;
}
public String toString() {
return "[" + key + ", " + value + "]";
}
} // 数据项
/**
* 构造查找表
*
* @param keys
* @param values
*/
public Search(int[] keys, String[] values) {
if (keys.length != values.length) {
throw new RuntimeException("键 与 值不对应!");
}
length = keys.length + 1;
searchTable = new DataItem[length];
searchTable[0] = new DataItem(-1, null);
for (int i = 1; i < length; i++) {
searchTable[i] = new DataItem(keys[i - 1], values[i - 1]);
}
} // structure
/**
* 打印查找表
*/
public String toString() {
String resultString = "I am a search table with " + (length - 1) + " data items.\r\n";
for (int i = 0; i < length; i++) {
resultString += searchTable[i] + " ";
}
return resultString;
}// toString
/**
* 顺序查找
*
* @param key
* @return key相对应的值
*/
public String sequentialSearch(int key) {
searchTable[0].key = key; // "哨兵"
int i;
for (i = length - 1; searchTable[i].key != key; i--)
;
return searchTable[i].value;
}
/**
* 折半查找
*
* @param key
* @return
*/
public String binarySearch(int key) {
int low = 1;
int high = length;
int mid;
while (low < high) {
mid = (low + high) / 2;
if (searchTable[mid].key == key) {
return searchTable[mid].value;
} else if (searchTable[mid].key < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return null;
}
public static void main(String[] args) {
System.out.println("\r\n-------sequentialSearchTest-------");
// 1
int[] keys = { 5, 3, 6, 10, 7, 1, 9 };
String[] values = { "if", "then", "else", "switch", "case", "for", "while" };
// 2
Search search = new Search(keys, values);
// 3
System.out.println(search.toString());
// 4
long startTime = System.nanoTime(); // 获取开始时间
System.out.println("Search result of 10 is: " + search.sequentialSearch(10));
long endTime = System.nanoTime(); // 获取结束时间
System.out.println("查找时间(纳秒): " + (endTime - startTime));
System.out.println("\r\n-------binarySearchTest-------");
// 1
int[] keys1 = { 1, 3, 5, 6, 7, 9, 10 };
String[] values1 = { "if", "then", "else", "switch", "case", "for", "while" };
// 2
Search search1 = new Search(keys1, values1);
// 3
System.out.println(search1.toString());
// 4
long sTime = System.nanoTime(); // 获取开始时间
System.out.println("Search result of 10 is: " + search1.binarySearch(10));
long eTime = System.nanoTime(); // 获取结束时间
System.out.println("查找时间(纳秒): " + (eTime - sTime));
}
} // Search