数据结构和算法系列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();        }    }}
复制代码

程序输出结果如图:

ds37

0 0