# 排序算法

## 常考排序

### 快速排序

```dart
void qs(List<int> nums, int left, int right) {
  if (left >= right) {
    return;
  }

  int start = left;
  int end = right;
  int pivot = nums[end];

  while (start <= end) {
    while (start <= end && nums[start] < pivot) {
      start++;
    }
    while (start <= end && nums[end] > pivot) {
      end--;
    }
    if (start <= end) {
      int temp = nums[start];
      nums[start] = nums[end];
      nums[end] = temp;
      start++;
      end--;
    }
  }
  qs(nums, left, end);
  qs(nums, start, right);
}
```

### 归并排序

```dart
void mergeSort(List<int> list, int start, int end, List<int> temp) {
  if (start >= end) {
    return;
  }
  int mid = start + (end - start) ~/ 2;
  mergeSort(list, start, mid, temp);
  mergeSort(list, mid + 1, end, temp);
  merge(list, start, end, temp);
}

void merge(List<int> list, int start, int end, List<int> temp) {
  int mid = start + (end - start) ~/ 2;

  int index = start;
  int leftIndex = start;
  int rightIndex = mid + 1;

  while (leftIndex <= mid && rightIndex <= end) {
    if (list[leftIndex] <= list[rightIndex]) {
      temp[index++] = list[leftIndex++];
    } else {
      temp[index++] = list[rightIndex++];
    }
  }

  while (leftIndex <= mid) {
    temp[index++] = list[leftIndex++];
  }

  while (rightIndex <= end) {
    temp[index++] = list[rightIndex++];
  }

  for (var i = start; i <= end; i++) {
    list[i] = temp[i];
  }
}
```

### 堆排序

用数组表示的完美二叉树 complete binary tree

> 完美二叉树 VS 其他二叉树

![image.png](https://img.fuiboom.com/img/tree_type.png)

[动画展示](https://www.bilibili.com/video/av18980178/)

![image.png](https://img.fuiboom.com/img/heap.png)

## 参考

[十大经典排序](https://www.cnblogs.com/onepixel/p/7674659.html)

[二叉堆](https://labuladong.gitbook.io/algo/shu-ju-jie-gou-xi-lie/er-cha-dui-xiang-jie-shi-xian-you-xian-ji-dui-lie)

## 练习

* [ ] 手写快排、归并


---

# Agent Instructions: 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:

```
GET https://ayaseeri.gitbook.io/algorithm-pattern-dart/ji-chu-suan-fa-pian/sort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
