索引查找是在索引表和主表(即线性表的索引存储结构)上进行的查找。
索引查找的过程是:
1) 首先根据给定的索引值K1,在索引表上查找出索引值等于KI的索引项,以确定对应予表在主表中的开始位置和长度,
2) 然后再根据给定的关键字K2,茬对应的子表中查找出关键字等于K2的元素(结点)。对索引表或子表进行查找时,若表是顺序存储的有序表,则既可进行顺序查找,也可进行二分查找,否则只能进行顺序查找。
一提到“索引”,估计大家第一反应就是“数据库索引”,对的,其实主键建立“索引”,就是方便我们在海量数据中查找。
实现索引查找时常使用的三个术语:
1) 主表:这个很简单,要查找的对象。
2) 索引项:一般我们会用函数将一个主表划分成几个子表,每个子表建立一个索引,这个索引叫做索引项。
3) 索引表:索引项的集合也就是索引表。
一般“索引项”包含三种内容:index,start,length
第一: 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位