> For the complete documentation index, see [llms.txt](https://wiki.hanzheteng.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.hanzheteng.com/algorithm/cs/divide-and-conquer.md).

# Divide and Conquer

### Three Steps

* **Divide** the problem into multiple subproblems in smaller size.
* **Conquer** the subproblems recursively; set a straight forward way for base case.
* **Combine** the solutions to the subproblems into the solution for the original problem.

### Master Theorem

* recurrence relation: $$T(n)=a T\left({\frac{n}{b}}\right)+f(n)$$
* $$a>1$$the number of subproblems in the recursion
* $$b>1$$the factor by which the subproblem size is reduced in each recursive call
* and$$f$$is asymptotically positive (positive for sufficiently large$$n$$)
* base case: $$T(c)$$is a constant when$$c$$is a constant
* compare $$n^{\log\_{b}{a}}$$with $$f(n)$$and this will lead to three regimes (>, =, <)
  * in recursion tree, compare root with leaves and see which part dominates
* more reference: CLRS book page 94

![](/files/-MccQZWgzUmY4K5lT2aY)

### Example Code: Merge Sort

```cpp
#include <iostream>
#include <vector>

int merge(std::vector<int>& A, int start, int end, int middle, std::vector<int>& B){
  // combine 
  int i = start;
  int j = middle;
  for (int k = start; k<= end; ++k){
    if (i < middle && (j > end || A[i] <= A[j]))
      B[k] = A[i++];
    else
      B[k] = A[j++];
  }
  for (int k = start; k<= end; ++k){
    A[k] = B[k];
  }
}

int mergeSort(std::vector<int>& A, int start, int end, std::vector<int>& B){
  // divide and conquer
  if (end == start)
    return 0;
  int middle = (start + end + 1) / 2;
  mergeSort(A, start, middle - 1, B);
  mergeSort(A, middle, end, B);
  merge(A, start, end, middle, B);
}

int main(){
  int n;
  std::cin >> n; 
  std::vector<int> score(n);
  std::vector<int> temp(n);
  for (int k = 0; k < n; ++k)
    std::cin >> score[k];

  mergeSort(score, 0, n-1, temp);

  std::cout << n << std::endl;
  for (int k = 0; k < n; ++k)
    std::cout << score[k] << std::endl;

  return 0;
}
```


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wiki.hanzheteng.com/algorithm/cs/divide-and-conquer.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
