数据结构和算法系列11 五大查找之索引查找
来源:互联网 发布:ai算法 编辑:程序博客网 时间:2024/06/06 16:42
这一篇我们要总结的是索引查找,关于索引,我们很容易地联想到数据库中的索引,建立了索引,可以大大提高数据库的查询速度。
索引查找又称为分块查找,是一种介于顺序查找和二分查找之间的一种查找方法,分块查找的基本思想是:首先查找索引表,可用二分查找或顺序查找,然后在确定的块中进行顺序查找。
分块查找的时间复杂度为O(√n)。
在实现索引查找算法前需要弄清楚以下三个术语。
1,主表。即要查找的对象。
2,索引项。一般我们会将主表分成几个子表,每个子表建立一个索引,这个索引就叫索引项。
3,索引表。即索引项的集合。
同时,索引项包括以下三点。
1,index,即索引指向主表的关键字。
2,start,即index在主表中的位置。
3,length,即子表的区间长度。
下面是算法实现代码。
C#版:
namespace IndexSearch.CSharp{ /// <summary> /// 索引项实体 /// </summary> public class IndexItem { public int Index { get; set; } public int Start { get; set; } public int Length { get; set; } } public class Program { /// <summary> /// 主表 /// </summary> static int[] studentList = new int[] { 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 }; /// <summary> /// 索引表 /// </summary> static IndexItem[] IndexItemList = new IndexItem[] { new IndexItem{ Index=1,Start=0,Length=5}, new IndexItem{ Index=2,Start=10,Length=4}, new IndexItem{ Index=3,Start=20,Length=3} }; /// <summary> /// 索引查找算法 /// </summary> /// <param name="key">给定值</param> /// <returns>给定值在表中的位置</returns> public static int IndexSearch(int key) { IndexItem item = null; // 建立索引规则 var index = key / 100; //遍历索引表,找到对应的索引项 for (int i = 0; i < IndexItemList.Count(); i++) { //找到索引项 if (IndexItemList[i].Index == index) { item = new IndexItem() { Start = IndexItemList[i].Start, Length = IndexItemList[i].Length }; break; } } //索引表中不存在该索引项 if (item == null) return -1; //在主表顺序查找 for (int i = item.Start; i < item.Start + item.Length; i++) { if (studentList[i] == key) { return i; } } return -1; } /// <summary> /// 插入数据 /// </summary> /// <param name="key"></param> /// <returns>true,插入成功,false,插入失败</returns> public static bool Insert(int key) { IndexItem item = null; try { //建立索引规则 var index = key / 100; int i = 0; //遍历索引表,找到对应的索引项 for (i = 0; i < IndexItemList.Count(); i++) { if (IndexItemList[i].Index == index) { item = new IndexItem() { Start = IndexItemList[i].Start, Length = IndexItemList[i].Length }; break; } } //索引表中不存在该索引项 if (item == null) return false; //依索引项将值插入到主表中 studentList[item.Start + item.Length] = key; //更新索引表 IndexItemList[i].Length++; } catch { return false; } return true; } static void Main(string[] args) { Console.WriteLine("********************索引查找(C#版)********************\n"); Console.WriteLine("原始数据:{0}",String.Join(" ",studentList)); int value = 205; Console.WriteLine("插入数据:{0}",value); //插入成功 if (Insert(value)) { Console.WriteLine("\n插入后数据:{0}",String.Join(",", studentList)); Console.WriteLine("\n元素205在列表中的位置为:{0} ",IndexSearch(value)); } Console.ReadKey(); } }}
程序输出结果如图:
0 0
- 数据结构和算法系列11 五大查找之索引查找
- 数据结构和算法系列12 五大查找之二叉排序树
- 数据结构和算法系列之 五大查找之哈希查找
- 数据结构和算法系列9 五大查找之顺序查找
- 数据结构和算法系列10 五大查找之二分查找
- 数据结构和算法系列13 五大查找之哈希查找
- 数据结构和算法系列13 五大查找之哈希查找
- C#数据结构和算法学习系列五----基础查找算法
- 数据结构之查找算法
- 数据结构之查找算法
- 数据结构之查找算法
- 数据结构之查找算法
- 数据结构之查找算法
- 算法系列——五大经典查找1
- 算法系列——五大经典查找2
- 算法系列——五大经典查找3
- 算法系列15天速成 五大经典查找
- 数据结构和算法------查找
- magento 解决上传文件名不能为中文的问题
- shell-sed
- 数据结构和算法系列10 五大查找之二分查找
- java 格式化计算时间差
- 学习Android的准备工作
- 数据结构和算法系列11 五大查找之索引查找
- 如何优化cocos2d程序的内存使用和程序大小
- do{}while(false);在java中的妙用.
- 数据结构和算法系列12 五大查找之二叉排序树
- Linux网卡驱动——网络Link状态检测&设备挂起恢复
- java学习笔记
- ORACLE,REDO01.LOG,REDO02.LOG,REDO03.LOG被删,或ORA-00313
- 在线聊天功能 实现中俄双语实时翻译 原创 加精!!
- 数据结构和算法系列13 五大查找之哈希查找