> For the complete documentation index, see [llms.txt](https://docs.1l.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.1l.lol/algorithms/divide-and-conquer.md).

# 分治算法

### 思想

分治（分而治之）把大问题**分解**成若干个规模更小的同类子问题，**递归求解**后再**合并**结果。

三步：**分解 → 解决 → 合并**。

### 经典应用

* **归并排序**：二分后合并两个有序段
* **快速排序**：以基准分区后递归
* **二分查找**：每次排除一半
* 求最大子段和、大整数乘法等

### 归并与快排的对比

|     | 归并排序       | 快速排序  |
| --- | ---------- | ----- |
| 分法  | 从中间等分      | 按基准值分 |
| 合并  | 需要合并       | 无需合并  |
| 稳定性 | 稳定         | 不稳定   |
| 最坏  | O(n log n) | O(n²) |

### 复杂度分析

若每次把问题分成规模 n/2 的两个子问题，合并代价 O(n)，则 `T(n) = 2T(n/2) + O(n)`，解得 **O(n log n)**。

### 分治与递归

分治通常用递归实现，但分治是一种**算法思想**，递归只是实现手段。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.1l.lol/algorithms/divide-and-conquer.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
