forked from greyireland/algorithm-pattern
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsort.md
140 lines (112 loc) · 3.08 KB
/
sort.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 排序
## 常考排序
### 快速排序
```c++
template<typename T>
static void QuickSort(T arr[], int len) {
quickSort(arr, 0, len - 1);
}
template<typename T>
static void quickSort(T arr[], int begin, int end) {
if (begin >= end) {
return;
}
auto pivot = partition(arr, begin, end);
quickSort(arr, begin, pivot - 1);
quickSort(arr, pivot + 1, end);
}
template<typename T>
static int partition(T arr[], int begin, int end) {
auto base = arr[end];
auto lessInsert = begin;
for (int i = begin; i < end; ++i) {
if (arr[i] < base) {
swap(arr[lessInsert++], arr[i]);
}
}
swap(arr[lessInsert], arr[end]);
return lessInsert;
}
```
### 归并排序
```c++
template<typename T>
static void MergeSort(T arr[], int len) {
auto tmp = new T[len];
mergeSort(arr, 0, len - 1, tmp);
delete[] tmp;
}
template<typename T>
static void mergeSort(T arr[], int begin, int end, T tmp[]) {
if (begin + 1 >= end) {
return;
}
auto mid = begin + (end - begin) / 2;
auto begin1 = begin;
auto end1 = mid;
auto begin2 = mid + 1;
auto end2 = end;
mergeSort(arr, begin1, end1, tmp);
mergeSort(arr, begin2, end2, tmp);
// merge two parts
auto index = begin;
while (begin1 <= end1 && begin2 <= end2) {
tmp[index++] = arr[begin1] < arr[begin2] ? arr[begin1++] : arr[begin2++];
}
while (begin1 <= end1) {
tmp[index++] = arr[begin1++];
}
while (begin2 <= end2) {
tmp[index++] = arr[begin2++];
}
for (int i = begin; i <= end; ++i) {
arr[i] = tmp[i];
}
}
```
### 堆排序
用数组表示的完美二叉树 complete binary tree
> 完美二叉树 VS 其他二叉树

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

核心代码
```c++
template<typename T>
static void HeapSort(T arr[], int len) {
// right leaf of a root = 2(n + 1) since n is start from 0
auto lastRoot = len / 2 - 1;
// 先自下而上构建堆
for (int i = lastRoot; i >= 0; --i) {
makeHeap(arr, i, len);
}
for (int j = len - 1; j >= 0; --j) {
// 最大值放到后面
swap(arr[0], arr[j]);
// 底部都还是有序的,只动了根节点,自上而下更新堆
makeHeap(arr, 0, --len);
}
}
template<typename T>
static void makeHeap(T arr[], int root, int len) {
auto left = root * 2 + 1;
auto right = (root + 1) * 2;
auto largest = root;
if (left < len && arr[left] > arr[largest]) {
largest = left;
}
if (right < len && arr[right] > arr[largest]) {
largest = right;
}
if (largest == root) {
return;
}
swap(arr[largest], arr[root]);
makeHeap(arr, largest, len);
}
```
## 参考
[十大经典排序](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)
## 练习
- [ ] 手写快排、归并、堆排序