Skip to content

Commit a16b923

Browse files
committed
update images
1 parent 430cee9 commit a16b923

File tree

1 file changed

+135
-15
lines changed

1 file changed

+135
-15
lines changed

content/09-datastruct-algorithm/08-sort.md

Lines changed: 135 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ tags: ["数据结构", "算法", "排序"]
2121
- 最坏时间复杂度:O(n^2)
2222
- 算法稳定性:稳定
2323

24-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-insert.jpg)
24+
![](https://s2.loli.net/2025/09/28/adm6QgkjbpZKsLR.png)
2525

2626
**优化**
2727
- 先用折半思想找到插入位置,再移动元素
2828

29-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-insert-better.jpg)
29+
![](https://s2.loli.net/2025/09/28/4pHYOm873lntMdJ.png)
3030

3131
## 希尔排序
3232

3333
### 算法思想
3434

3535
- 先追求表中元素部分有序,再逼近全局有序
3636

37-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-shell-base.jpg)
37+
![](https://s2.loli.net/2025/09/28/za9nYixGVXRrmWK.png)
3838

3939
### 代码实现
4040

4141
- 最坏时间复杂度:O(n^2),当n在某个范围时,可达O(n^1.3)
4242
- 算法稳定性:不稳定
4343

44-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-shell.jpg)
44+
![](https://s2.loli.net/2025/09/28/IL9V1AhaYrKvRoN.png)
4545

4646
## 冒泡排序
4747

@@ -55,8 +55,8 @@ tags: ["数据结构", "算法", "排序"]
5555
- 最坏时间复杂度:O(n^2) 逆序的情况
5656
- 算法稳定性:稳定
5757

58-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-bubble-swap.jpg)
59-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-bubble.jpg)
58+
![](https://s2.loli.net/2025/09/28/6qE4LH9oSNCzY35.png)
59+
![](https://s2.loli.net/2025/09/28/qxbsciVjuTYeyHQ.png)
6060

6161
## 快速排序
6262

@@ -77,9 +77,41 @@ tags: ["数据结构", "算法", "排序"]
7777
- 算法稳定性:不稳定
7878
- 所有排序算法中平均性能最优的
7979

80-
81-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-quick-part.jpg)
82-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-quick.jpg)
80+
```c++
81+
// 用第一个元素将待排序序列划分成左右两个部分
82+
int Partition(int A[], int low, int high) {
83+
// 第一个元素作为枢轴
84+
int pivot = A[low];
85+
// 用low, high搜索枢轴的最终位置
86+
while (low < high) {
87+
while (low < high && A[high] >= pivot) --high;
88+
// 比枢轴小的元素移动到左端
89+
A[low] = A[high];
90+
while (low < high && A[low] <= pivot) ++low;
91+
// 比枢轴大的元素移动到右端
92+
A[high] = A[low];
93+
}
94+
// 枢轴元素存放到最终位置
95+
A[low] = pivot;
96+
// 返回存放枢轴的最终位置
97+
return low;
98+
}
99+
```
100+
101+
```c++
102+
// 快速排序
103+
void QuickSort(int A[], int low, int high) {
104+
// 递归出口的条件
105+
if (low < high) {
106+
// 划分
107+
int pivotpos = Partition(A, low, high);
108+
// 划分左子表
109+
QuickSort(A, low, pivotpos - 1);
110+
// 划分右子表
111+
QuickSort(A, pivotpos + 1, high);
112+
}
113+
}
114+
```
83115

84116
## 选择排序
85117

@@ -92,7 +124,22 @@ tags: ["数据结构", "算法", "排序"]
92124
- 时间复杂度:O(n^2)
93125
- 算法稳定性:不稳定
94126

95-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-select.jpg)
127+
```c++
128+
// 简单选择排序
129+
void SelectSort(int A[], int n) {
130+
// 一共进行n-1趟
131+
for (int i = 0; i < n - 1; i++) {
132+
// 记录最小元素位置
133+
int min = i;
134+
// 在A[i...n-1]中选择最小的元素
135+
for (int j = i + 1; j < n; j++)
136+
// 更新最小元素位置
137+
if (A[j] < A[min]) min = j;
138+
// 封装的swap()函数交换元素3次
139+
if (min != i) swap(A[i], A[min]);
140+
}
141+
}
142+
```
96143
97144
## 堆排序
98145
@@ -108,8 +155,36 @@ tags: ["数据结构", "算法", "排序"]
108155
109156
### 最大堆代码
110157
111-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-max-heap-build.jpg)
112-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-max-heap-head.jpg)
158+
```c++
159+
// 建立大根堆
160+
void BuildMaxHeap(int A[], int len) {
161+
// 从后往前调整所有非终端结点
162+
for (int i = len/2; i > 0; i--)
163+
HeadAdjust(A, i, len);
164+
}
165+
166+
// 将以k为根的子树调整为大根堆
167+
void HeadAdjust(int A[], int k, int len) {
168+
// A[0]暂存子树的根结点
169+
A[0] = A[k];
170+
// 沿key较大的子结点向下筛选
171+
for (int i = 2*k; i <= len; i *= 2) {
172+
// 取key较大的子结点的下标
173+
if (i < len && A[i] < A[i+1])
174+
i++;
175+
// 筛选结束
176+
if (A[0] >= A[i]) break;
177+
else {
178+
// 将A[i]调整到双亲结点上
179+
A[k] = A[i];
180+
// 修改k值,以便继续向下筛选
181+
k = i;
182+
}
183+
}
184+
// 被筛选结点的值放入最终位置
185+
A[k] = A[0];
186+
}
187+
```
113188

114189
### 最大堆排序
115190

@@ -121,7 +196,21 @@ tags: ["数据结构", "算法", "排序"]
121196
- 排序时间复杂度:O($n\log_2n$)
122197
- 算法稳定性:不稳定
123198

124-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-max-heap.jpg)
199+
200+
```c++
201+
// 堆排序的完整逻辑
202+
void HeapSort(int A[], int len) {
203+
// 初始建堆
204+
BuildMaxHeap(A, len);
205+
// n-1趟的交换和建堆过程
206+
for (int i = len; i > 1; i--) {
207+
// 堆顶元素和堆底元素交换
208+
swap(A[i], A[1]);
209+
// 把剩余的待排序元素整理成堆
210+
HeadAdjust(A, 1, i-1);
211+
}
212+
}
213+
```
125214
126215
### 插入和删除
127216
@@ -153,8 +242,39 @@ tags: ["数据结构", "算法", "排序"]
153242
- 空间复杂度:O(n)
154243
- 算法稳定性:稳定
155244
156-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-merge-sort.jpg)
157-
![](https://jihulab.com/xnzone/earth-bear/-/raw/master/sort-merge.jpg)
245+
```c++
246+
void MergeSort(int A[], int low, int high) {
247+
if (low < high) {
248+
// 从中间划分
249+
int mid = (low + high) / 2;
250+
// 对左半部分归并排序
251+
MergeSort(A, low, mid);
252+
// 对右半部分归并排序
253+
MergeSort(A, mid + 1, high);
254+
// 归并
255+
Merge(A, low, mid, high);
256+
} // if
257+
}
258+
```
259+
260+
```c++
261+
// A[low...mid]和A[mid+1...high]各自有序,将两个部分归并
262+
void Merge(int A[], int low, int mid, int high) {
263+
int i, j, k;
264+
// 将A中所有元素复制到B中
265+
for (k = low; k <= high; k++)
266+
B[k] = A[k];
267+
for (i = low, j = mid + 1, k = i; i <= mid && j <= high; k++) {
268+
if (B[i] <= B[j])
269+
// 将较小值复制到A中
270+
A[k] = B[i++];
271+
else
272+
A[k] = B[j++];
273+
} // for
274+
while (i <= mid) A[k++] = B[i++];
275+
while (j <= high) A[k++] = B[j++];
276+
}
277+
```
158278
159279
## 基数排序
160280

0 commit comments

Comments
 (0)