算法之二分法


算法简介之二分法(需要写的出来)

一、简介

关于算法我们只需要稍微了解一下就可以了,对于算法,刚入行的小白基本上是接触不到相关工作的,除了顶尖高效的相关专业可以直接找到工作,通常情况下需要我们在进入一些大公司工作一段时间后,被调到算法部门,才是正常的工作流程。对于算法这个东西,小公司不会成立相关部门研究算法,只有大公司才会成立部门研究算法,因为算法产出低,但是算法从业者的工资又相对偏高。小公司中的算法部门大多数情况都是拿别人的算法来改改用用的。

二、什么是算法

算法的范围很大,只要是解决问题的思路都算算法。但是不是所有的算法都是高效的,也有很多不合格的算法,可这并不影响算法的流行。因为算法的应用范围确实很广,几乎涵盖了我们生活中的方方面面:推荐算法(抖音视频推送 淘宝商品推送)、成像算法(AI相关)

常见算法的原理以及伪代码:

二分法、冒泡、快拍、插入、堆排、桶排、数据结构(链表 约瑟夫问题 如何链表是否成环)

三、二分法

二分法算是算法中最简单的算法,有很大缺陷,但是面试的时候喜欢拿来考一考。

二分法有以下限制条件:

1、进行运算的数据值需要是有序的。

2、运算的时候如果需要取得的数据值是在两头的,反而会更加耗时

# 查找列表中某个数据值
# 方式1:for循环  次数较多
# 方式2:二分法 不断的对数据集做二分切割
correct = 754
# 假设我们要找到啊754这个值

# 获取中间的索引,这里通过整除来弄
def find(l1):
    # 如果列表中没有数据值就说明查找失败了
    if len(l1) == 0:
        return '没有找到'
    middle = len(l1) // 2
    if l1[middle] > correct:
        # 如果中间这个数的值大于要找的这个值就取列表左边的列表继续查找
        new_list = l1[:middle]
        print(new_list)
        return find(new_list)
    elif l1[middle] < correct:
        # 如果中间这个数的值小于要找的这个值就取列表右边的列表继续查找
        new_list = l1[middle:]
        print(new_list)
        return find(new_list)
    else:
        # 除了大于和小于两种情况就是找到了那个值
        return correct


print(find(l1))


posted @   致丶幻  阅读(63)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
  1. 1 So Far Away (Acoustic) Adam Christopher
  2. 2 雪 Distance Capper&罗言RollFlash
  3. 3 CollapsingWorld
  4. 4 Call You Tonight Johnta Austin
So Far Away (Acoustic) - Adam Christopher
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作曲 : David Guetta/Giorgio H Tuinfort/Jamie Scott/Martijn G Garritsen

Light ‘em up, light ‘em up

Tell me where you are, tell me where you are

The summer nights, the bright lights

And the shooting stars, they break my heart

I‘m calling you now, but you‘re not picking up

Your shadows so close if you are still in love

Then light a match, light a match

Baby, in the dark, show me where you are

Oh, love

How I miss you every single day when I see you on those streets

Oh, love

Tell me there‘s a river I can swim that will bring you back to me

‘Cause I don‘t know how to love someone else

I don‘t know how to forget your face

Oh, love

God, I miss you every single day and now you‘re so far away

It‘s breaking me, I‘m losing you

We were far from perfect, but we were worth it

Too many fights, and we cried, but never said we‘re sorry

Stop saying you love me

You‘re calling me now, but I can‘t pick up

Your shadow‘s too close, and I‘m still in love

The summer‘s over now, but somehow, it still breaks my heart

We could have had the stars, oh

Oh, love

How I miss you every single day when I see you on those streets

Oh, love

Tell me there‘s a river I can swim that will bring you back to me

‘Cause I don‘t know how to love someone else

I don‘t know how to forget your face

Oh, love

God, I miss you every single day and now you‘re so far away

Oh, love

How I miss you every single day when I see you on those streets

Oh, love

Tell me there‘s a river I can swim that will bring you back to me

‘Cause I don‘t know how to love someone else

I don‘t know how to forget your face

Oh, love

God, I miss you every single day when you‘re so far away

点击右上角即可分享
微信分享提示