---
title: "大 O 时间复杂度与二分查找 (Beginner–Pro · EGE) — 编程与计算机科学 | FlyTalkAI"
description: "理解为何 O(1) 哈希查找与 O(log n) 二分查找远胜于 O(n²) 嵌套循环。"
canonical_url: "https://flytalkai.com/zh/learn/programming/algorithms-python/big-o-and-binary-search"
locale: zh
meta:
  "og:description": "理解为何 O(1) 哈希查找与 O(log n) 二分查找远胜于 O(n²) 嵌套循环。"
  "og:image": "https://flytalkai.com/img/social/tutor-zh.png"
  "og:image:alt": "蓝色 FlyTalkAI 导师吉祥物，周围有书籍、语言和科学符号"
  "og:image:height": "630"
  "og:image:width": "1200"
  "og:title": "大 O 时间复杂度与二分查找 | FlyTalkAI"
  "twitter:description": "与专属 AI 导师一起学习外语、数学、理科与编程。支持语音对话、互动课程、水平测试、词汇记忆与错题讲解，立即免费体验。"
  "twitter:image": "https://flytalkai.com/img/social/tutor-zh.png"
  "twitter:image:alt": "蓝色 FlyTalkAI 导师吉祥物，周围有书籍、语言和科学符号"
  "twitter:title": "在线 AI 导师 – 外语与学科一对一学习"
---

> Index of every page on this site: [llms.txt](https://flytalkai.com/llms.txt)

编程与计算机科学 · Beginner–Pro · EGE

# 大 O 时间复杂度与二分查找

理解为何 O(1) 哈希查找与 O(log n) 二分查找远胜于 O(n²) 嵌套循环。

[用 AI 导师练习本主题](https://flytalkai.com/zh/programming)

## 规则与表格

二分查找要求数组“已排序”，每一步将搜索区间减半，时间复杂度为 O(log₂ n)——在 1,000,000 个元素中查找目标最多只需 20 次比较。

| 复杂度 | 算法 / 操作 | n = 1,000 时的步数 |
| --- | --- | --- |
| O(1) | Hash table / Python dict lookup | 1 |
| O(log n) | Binary search (sorted array) | \~10 |
| O(n) | Linear scan through list | 1,000 |
| O(n log n) | Merge sort / Timsort (sorted()) | \~10,000 |
| O(n²) | Nested loops / Bubble sort | 1,000,000 |

## 实用例句

### mid = (left + right) // 2

Python 二分查找中的中间索引

## 常见错误

✗ 错误: Running binary search on an unsorted list

✓ 正确: Sort first or use a hash set / dict for O(1) lookup

如果序列不是单调有序的，二分查找会得出错误结果。

## 小测验

在 Python 的 dict（哈希表）中查找一个键的平均时间复杂度是多少？

## 用 AI 导师练习本主题

FlyTalkAI 会批改你写的真实句子、解析错误，并针对这一主题生成自适应巩固练习。

[在 FlyTalkAI 中继续学习](https://flytalkai.com/zh/programming)

```json
{"@context":"https://schema.org","@graph":[{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"首页","item":"https://flytalkai.com/zh"},{"@type":"ListItem","position":2,"name":"学习地图","item":"https://flytalkai.com/zh/learn"},{"@type":"ListItem","position":3,"name":"编程与计算机科学","item":"https://flytalkai.com/zh/learn/programming"},{"@type":"ListItem","position":4,"name":"Beginner–Pro · EGE","item":"https://flytalkai.com/zh/learn/programming/algorithms-python"},{"@type":"ListItem","position":5,"name":"大 O 时间复杂度与二分查找","item":"https://flytalkai.com/zh/learn/programming/algorithms-python/big-o-and-binary-search"}]},{"@type":"LearningResource","name":"大 O 时间复杂度与二分查找","description":"理解为何 O(1) 哈希查找与 O(log n) 二分查找远胜于 O(n²) 嵌套循环。","educationalLevel":"Beginner–Pro · EGE","learningResourceType":"Interactive Lesson","inLanguage":"zh","url":"https://flytalkai.com/zh/learn/programming/algorithms-python/big-o-and-binary-search","provider":{"@type":"Organization","name":"FlyTalkAI","url":"https://flytalkai.com"}}]}
```

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://flytalkai.com/zh#website","@type":"WebSite","inLanguage":"zh-CN","name":"FlyTalkAI","url":"https://flytalkai.com/zh","translationOfWork":{"@id":"https://flytalkai.com/#website"}},{"@id":"https://flytalkai.com/zh/learn/programming/algorithms-python/big-o-and-binary-search#webpage","@type":"WebPage","description":"理解为何 O(1) 哈希查找与 O(log n) 二分查找远胜于 O(n²) 嵌套循环。","name":"大 O 时间复杂度与二分查找 (Beginner–Pro · EGE) — 编程与计算机科学 | FlyTalkAI","url":"https://flytalkai.com/zh/learn/programming/algorithms-python/big-o-and-binary-search","isPartOf":{"@id":"https://flytalkai.com/zh#website"},"potentialAction":[{"@type":"ReadAction","target":["https://flytalkai.com/zh/learn/programming/algorithms-python/big-o-and-binary-search"]}]}]}
```

## Sitemap

See the full [sitemap](https://flytalkai.com/sitemap.md) for all pages.
