查找算法整理之索引查找

来源:互联网 发布:python辗转相减法 编辑:程序博客网 时间:2024/04/30 20:13

索引查找是在索引表和主表(即线性表的索引存储结构)上进行的查找。

索引查找的过程是:

1)       首先根据给定的索引值K1,在索引表上查找出索引值等于KI的索引项,以确定对应予表在主表中的开始位置和长度,

2)       然后再根据给定的关键字K2,茬对应的子表中查找出关键字等于K2的元素(结点)。对索引表或子表进行查找时,若表是顺序存储的有序表,则既可进行顺序查找,也可进行二分查找,否则只能进行顺序查找。

 

一提到“索引”,估计大家第一反应就是“数据库索引”,对的,其实主键建立“索引”,就是方便我们在海量数据中查找。

 

实现索引查找时常使用的三个术语:

1)       主表:这个很简单,要查找的对象。

2)       索引项:一般我们会用函数将一个主表划分成几个子表,每个子表建立一个索引,这个索引叫做索引项。

3)       索引表:索引项的集合也就是索引表。

 

一般“索引项”包含三种内容:indexstartlength

第一: index,也就是索引指向主表的关键字。

第二:start,也就是index在主表中的位置。

第三:length,也就是子表的区间长度。

 

代码实现:

 

public class IndexSearch {// 主表static int[] students = { 101, 102, 103, 104, 105, 0, 0, 0, 0, 0, 201, 202,203, 204, 0, 0, 0, 0, 0, 0, 301, 302, 303, 0, 0, 0, 0, 0, 0, 0 };// 索引表static IndexItem[] indexItem = { new IndexItem(1, 0, 5),new IndexItem(2, 10, 4), new IndexItem(3, 20, 3), };// 查找数据public static int indexSearch(int key) {IndexItem item = null;// 建立索引规则int index = key / 100;// 首先去索引找for (int i = 0; i < indexItem.length; i++) {if (indexItem[i].index == index) {item = new IndexItem(index, indexItem[i].start,indexItem[i].length);break;}}// 如果item为null,则说明在索引中查找失败if (item == null)return -1;for (int i = item.start; i < item.start + item.length; i++) {if (students[i] == key) {return i;}}return -1;}// / 插入数据public static int insert(int key) {IndexItem item = null;// 建立索引规则int index = key / 100;int i = 0;for (i = 0; i < indexItem.length; i++) {// 获取到了索引if (indexItem[i].index == index) {item = new IndexItem(index, indexItem[i].start,indexItem[i].length);break;}}if (item == null)return -1;// 更新主表students[item.start + item.length] = key;// 更新索引表indexItem[i].length++;return 1;}public static void main(String[] args) {int value = 205;// 将205插入集合中,过索引int index = insert(value);insert(308);// 如果插入成功,获取205元素所在的位置if (index == 1) {System.out.println("\n插入后数据:" + Arrays.toString(students));System.out.println("\n数据元素:205在数组中的位置为 " + indexSearch(205) + "位");}}}// 索引项实体class IndexItem {// 对应主表的值public int index;// 主表记录区间段的开始位置public int start;// 主表记录区间段的长度public int length;public IndexItem() {}public IndexItem(int index, int start, int length) {this.index = index;this.start = start;this.length = length;}}运行结果:原数据为:[101, 102, 103, 104, 105, 0, 0, 0, 0, 0, 201, 202, 203, 204, 0, 0, 0, 0, 0, 0, 301, 302, 303, 0, 0, 0, 0, 0, 0, 0]插入后数据:[101, 102, 103, 104, 105, 0, 0, 0, 0, 0, 201, 202, 203, 204, 205, 0, 0, 0, 0, 0, 301, 302, 303, 308, 0, 0, 0, 0, 0, 0]数据元素:205在数组中的位置为 14位


 

原创粉丝点击