惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
Intezer
D
DataBreaches.Net
罗磊的独立博客
P
Proofpoint News Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
Stack Overflow Blog
Stack Overflow Blog
S
Security @ Cisco Blogs
博客园 - Franky
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LINUX DO - 最新话题
SecWiki News
SecWiki News
H
Help Net Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Vercel News
Vercel News
G
Google Developers Blog
TaoSecurity Blog
TaoSecurity Blog
B
Blog
I
InfoQ
V
Visual Studio Blog
S
Security Affairs
Help Net Security
Help Net Security
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
N
News | PayPal Newsroom
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
Engineering at Meta
Engineering at Meta
PCI Perspectives
PCI Perspectives
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
美团技术团队
S
Secure Thoughts
F
Fortinet All Blogs
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
人人都是产品经理
人人都是产品经理
V
Vulnerabilities – Threatpost
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
T
Threat Research - Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
宝玉的分享
宝玉的分享
F
Full Disclosure

Nemo

再见,2025 Complete ORB-SLAM3 Setup Guide for Jetson Xavier NX with RealSense D455 20250723 再见,2024 Using CertBot for Automatic Secure EMQX Broker Create Your Own GPS Data Publisher Support SSL AGV Dispatching System Technical Documentation Finding Nemo No title 香港賽馬會呈獻系列:黑白——攝影敘事 再见,2023 团团是只猫 Design an FSM for Robot State Machines – Basics of Computer Science Data-driven robot lifespan: Collection 数据驱动的机器人寿命:收集、诊断、预测 Exploring the design space of binary search trees 特首来了 Large language models, explained with a minimum of math and jargon 设计有缓存异步逻辑的监控脚本并测试其资源占用 使用loguru记录串口数据并使用Docker搭建ARM开发环境 在vscode的Dev Container中构建.NET开发环境及使用doxygen和graphviz绘制函数调用图 State or Status? A*算法两种时间复杂度 /A* Algorithm: Two Types of Time Complexity 使用KD-Tree快速收敛到最近坐标点/Fast convergence to the nearest coordinate point using KD-Tree 翻译 || 总结 - Go语言中的空结构体(The empty struct) 再见,2022 从PE工作报告中能读出什么 Give me miles, give me truth AMR调度系统性能优化/AMR Dispatch System Performance Optimization 条件触发AMR避让流程/Conditionally triggered AMR avoidance process Docker实现调度系统整体部署/Docker implementation of dispatching system overall deployment 调度系统中加密算法的使用/增加SM4补0方法/Use of encryption algorithms in scheduling systems/add SM4 complementary 0 method 一篇关于北京四天三夜的攻略 2022 藏疆自驾 策划/招募书 AMR仿真模型/AMR Simulation Model 东东有鱼2022年会分享 使用perf-FlameGraph监控系统性能 Beyond Compare 4删除试用 你的灵魂有香气 Pyinstaller打包Python项目 [转载]Python中的单例模式的几种实现方式的及优化 - Nemo 再见! 2020 1024 UML软件建模 入职优必选一个月 写论文时 那些让你开心的软件 - Nemo 分享一个记录刷题次数的模版 LeetCode-查找表类算法题精析 PyCharm 调用vs 2010 C++库导致提示报错R6034解决方法 MySQL基础教程 多种数据结构的Python实现形式 字节跳动-挑战字符串 选择排序 算法中的动态规划问题 高高手课程-青山裕企人像摄影 笔记(内含福利)
二分查找和大O表示法
Nemo · 2019-05-12 · via Nemo

本章内容

  • 编写第一种查找算法:二分查找
  • 学习如何谈论算法的运行时间:大O表示法
  • 了解一种常用的设计方法:递归

什么是算法?

  • 算法是一组完成任务的指令,任何代码片段都可以视为算法。

二分查找

  • 二分查找是一种算法,其输入是一个有序的元素列表(仅当列表有序的时候,二分查找才管用)。如果要查找的元素包含在列表中,二分查找返回其位置,否则返回null。
首先介绍简单查找

简单查找就是‘傻找’,例如猜测一个1~100的数字,从1开始依次往上猜,每次猜测只能排除一个数字。如果数字是99,则需要猜测99次才能猜到。

二分查找
  • 而二分查找法则从50开始,一下子可以排除一半的数字;每次都猜测中间的数字,从而每次都将剩余的数字排除一半。
  • 一般而言,对于包含n个元素的列表,用二分查找最多需要[latex]\log n[/latex]步,而简单查找最多需要n步。

二分查找Python代码实现
def binary_search(list, item):
    low = 0
    high = len(list) - 1 
    while low <= high: #只要范围没有缩小到只包含一个元素,就检查中间的元素
        mid = (low + high) // 2
        guess = list[mid]
        if guess == item: #找到元素了
            return mid
        if guess > item: #猜的值大了
            high = mid - 1
        else: #猜的值小了
            low = mid + 1
    return None #没有猜测的值


list = [1, 2, 3, 4, 5, 6, 7]

# 能找到对应的值
print(binary_search(list, 3))
print(binary_search(list, 6))

# 不能找到对应的值
print(binary_search(list, 8))

leetcode中的二分查找

运行时间

  • 线性时间(linear time):以简单查找为例,最多的猜测次数与列表长度相同,这被称为线性时间-O(n)。
  • 对数时间(log时间)::比如二分查找-O(log n).

线性时间和对数时间的增速不同!

大O表示法

大O表示法是一种特殊的表示法,指出了算法的速度有多快,同时也表示了算法的增速的差异,其单位并不是秒,而是操作数,指出了最糟糕情况下的运行时间。

一些常见的大O运行时间

从快到慢:
O(log n) 对数时间,如二分查找
O(n) 线性时间,如简单查找
O(n * log n ) 线性对数时间,如快速排序
O([latex]n^{2}[/latex]) 平方时间,如选择排序
O(n!) 阶乘时间,如旅行商问题

小结
  • 算法的速度指的并非时间,而是操作数的增速
  • 谈论算法的速度时,我们说的是随着输入的增加,其运行时间将以什么样的速度增加。
  • 算法的运行时间用大O表示法表示
  • O(log n)比O(n)快,当需要搜索的元素越多时,前者比后者快的越多。