博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构和算法系列11 五大查找之索引查找
阅读量:7296 次
发布时间:2019-06-30

本文共 3706 字,大约阅读时间需要 12 分钟。

这一篇我们要总结的是索引查找,关于索引,我们很容易地联想到数据库中的索引,建立了索引,可以大大提高数据库的查询速度。

索引查找又称为分块查找,是一种介于顺序查找和二分查找之间的一种查找方法,分块查找的基本思想是:首先查找索引表,可用二分查找或顺序查找,然后在确定的块中进行顺序查找。

分块查找的时间复杂度为O(√n)。

在实现索引查找算法前需要弄清楚以下三个术语。

1,主表。即要查找的对象。

2,索引项。一般我们会将主表分成几个子表,每个子表建立一个索引,这个索引就叫索引项。

3,索引表。即索引项的集合。

同时,索引项包括以下三点。

1,index,即索引指向主表的关键字。

2,start,即index在主表中的位置。

3,length,即子表的区间长度。

下面是算法实现代码。

C#版:

namespace IndexSearch.CSharp{    ///     /// 索引项实体    ///     public class IndexItem    {        public int Index { get; set; }         public int Start { get; set; }        public int Length { get; set; }    }    public class Program    {        ///         /// 主表        ///         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        };        ///         /// 索引表        ///         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}        };        ///         /// 索引查找算法        ///         /// 给定值        /// 
给定值在表中的位置
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; } /// /// 插入数据 /// /// ///
true,插入成功,false,插入失败
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(); } }}

程序输出结果如图:

转载地址:http://ooynm.baihongyu.com/

你可能感兴趣的文章
cmake是什么
查看>>
使用MASM10(变量的使用)- Win32汇编语言018
查看>>
【Docker学习笔记】----基于centos 7 的Docker安装
查看>>
Android笔记之OnLongClickListener
查看>>
Java客户端:调用EyeKey HTTP接口进行人脸对比
查看>>
SQL之分区函数
查看>>
创业公司如何实施敏捷开发
查看>>
Django使用AJAX调用自己写的API接口
查看>>
数据科学求职准备
查看>>
Wireshark抓包工具使用教程以及常用抓包规则
查看>>
fedora16下更改网卡名字
查看>>
awk中NF,NR的含义
查看>>
Centos下Docker中运行neo4j 并配置挂载本地文件
查看>>
静态页面跳转传值小插件
查看>>
JetBrains公司的IDE使用技巧
查看>>
第三届中国云计算用户大会笔记和心得
查看>>
PHP7开启opcache打造强悍性能
查看>>
加载某个页面(A)时实现自动跳转到某个页面(B)
查看>>
Jenkins入门系列之——03PDF文档下载
查看>>
Digit Generator(生成元)
查看>>