算法系列15天速成 第四天 五大经典查找【上】

时间:2023-12-18 01:20:38 

在我们的算法中,有一种叫做线性查找。

分为:顺序查找。
        折半查找。

查找有两种形态:

分为:破坏性查找,   比如有一群mm,我猜她们的年龄,第一位猜到了是23+,此时这位mm已经从我脑海里面的mmlist中remove掉了。

                            哥不找23+的,所以此种查找破坏了原来的结构。

       非破坏性查找, 这种就反之了,不破坏结构。

顺序查找:

    这种非常简单,就是过一下数组,一个一个的比,找到为止。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sequential
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 2, 3, 5, 8, 7 };

            var result = SequenceSearch(list, 3);

            if (result != -1)
                Console.WriteLine("3 已经在数组中找到,索引位置为:" + result);
            else
                Console.WriteLine("呜呜,没有找到!");

            Console.Read();
        }

        //顺序查找
        static int SequenceSearch(List<int> list, int key)
        {
            for (int i = 0; i < list.Count; i++)
            {
                //查找成功,返回序列号
                if (key == list[i])
                    return i;
            }
            //未能查找,返回-1
            return -1;
        }
    }
}

算法系列15天速成 第四天 五大经典查找【上】

折半查找: 这种查找很有意思,就是每次都砍掉一半,

             比如"幸运52“中的猜价格游戏,价格在999元以下,1分钟之内能猜到几样给几样,如果那些选手都知道折半查找,
             那结果是相当的啊。

不过要注意,这种查找有两个缺点:

            第一: 数组必须有序,不是有序就必须让其有序,大家也知道最快的排序也是NLogN的,所以.....呜呜。
            第二: 这种查找只限于线性的顺序存储结构。

上代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BinarySearch
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 3, 7, 9, 10, 11, 24, 45, 66, 77 };

            var result = BinarySearch(list, 45);

            if (result != -1)
                Console.WriteLine("45 已经在数组中找到,索引位置为:" + result);
            else
                Console.WriteLine("呜呜,没有找到!");

            Console.Read();
        }

        ///<summary>
/// 折半查找
///</summary>
///<param name="list"></param>
///<returns></returns>
        public static int BinarySearch(List<int> list, int key)
        {
            //最低线
            int low = 0;

            //最高线
            int high = list.Count - 1;

            while (low <= high)
            {
                //取中间值
                var middle = (low + high) / 2;

                if (list[middle] == key)
                {
                    return middle;
                }
                else
                    if (list[middle] > key)
                    {
                        //下降一半
                        high = middle - 1;
                    }
                    else
                    {
                        //上升一半
                        low = middle + 1;
                    }
            }
            //未找到
            return -1;
        }
    }
}

算法系列15天速成 第四天 五大经典查找【上】

先前也说过,查找有一种形态是破坏性的,那么对于线性结构的数据来说很悲惨,因为每次破坏一下,

可能都导致数组元素的整体前移或后移。

    所以线性结构的查找不适合做破坏性操作,那么有其他的方法能解决吗?嗯,肯定有的,不过要等下一天分享。

ps:  线性查找时间复杂度:O(n);
         折半无序(用快排活堆排)的时间复杂度:O(NlogN)+O(logN);
         折半有序的时间复杂度:O(logN);

标签:经典查找
0
投稿

猜你喜欢

  • 使用FCKeditor添加文章时,在文章最后多了逗号

    2007-10-11 13:38:00
  • Java连接Mysql 8.0.18版本的方法详解

    2024-01-24 10:08:29
  • pip如何用pipdeptree查看包依赖

    2022-07-28 01:56:26
  • Python新版极验验证码识别验证码教程详解

    2022-03-07 01:02:55
  • 一文弄懂Pytorch的DataLoader, DataSet, Sampler之间的关系

    2022-06-27 14:21:53
  • 几种MySQL中的联接查询操作方法总结

    2024-01-25 05:15:15
  • win10 + anaconda3 + python3.6 安装tensorflow + keras的步骤详解

    2021-05-18 10:22:32
  • php开启mysqli扩展之后如何连接数据库

    2023-11-14 17:36:08
  • GoJs的文本绘图模板TextBlock使用实例

    2023-08-26 16:01:00
  • MySQL Delete 删数据后磁盘空间未释放的原因

    2024-01-23 10:56:14
  • Python 定义只读属性的实现方式

    2023-04-28 01:11:22
  • 7个Python中的隐藏小技巧分享

    2022-06-20 22:36:14
  • 使用实现pandas读取csv文件指定的前几行

    2021-06-28 12:05:26
  • python使用KNN算法识别手写数字

    2022-02-20 10:48:23
  • Python中的list.sort()方法和函数sorted(list)

    2021-07-29 17:02:23
  • mysql+Spring数据库隔离级别与性能分析

    2024-01-15 00:46:58
  • oracle 发送邮件 实现方法

    2009-06-10 17:49:00
  • MySQL数据库临时文件究竟储存在哪里

    2009-09-06 12:11:00
  • 用python做游戏的细节详解

    2022-02-08 05:18:39
  • HTML5 第二份草案发布

    2008-06-17 17:42:00
  • asp之家 网络编程 m.aspxhome.com