zoukankan      html  css  js  c++  java
  • 数据结构和算法系列11 五大查找之索引查找

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

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

    分块查找的时间复杂度为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#版)********************
    ");
                Console.WriteLine("原始数据:{0}",String.Join(" ",studentList));
    
                int value = 205;
                Console.WriteLine("插入数据:{0}",value);
                //插入成功
                if (Insert(value))
                {
                    Console.WriteLine("
    插入后数据:{0}",String.Join(",", studentList));
                    Console.WriteLine("
    元素205在列表中的位置为:{0} ",IndexSearch(value));
                }
    
                Console.ReadKey();
            }
        }
    }

    程序输出结果如图:

    ds37

  • 相关阅读:
    js_未结束的字符串常量
    [转]关于项目管理的思考
    Nhibernate理解
    Visual Studio 2005常用插件搜罗
    基本概念
    resharper 2.0
    Nhibernate资源
    [转]关于项目管理的知识点
    style
    带分数 蓝桥杯
  • 原文地址:https://www.cnblogs.com/mcgrady/p/3271609.html
Copyright © 2011-2022 走看看