Skip to content

Commit 04cf5b9

Browse files
committed
m
1 parent b7a0837 commit 04cf5b9

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

刷题/排序算法.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

刷题/排序算法总结.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<!-- GFM-TOC -->
22
* [选择排序](#-选择排序)
33
* [冒泡排序](#-冒泡排序)
4+
* [快速排序](#-快速排序)
5+
* [插入排序](#-插入排序)
46
<!-- GFM-TOC -->
57

68
# 选择排序
@@ -110,4 +112,42 @@ int main()
110112
system("pause");
111113
return 0;
112114
}
115+
```
116+
117+
# 快速排序
118+
快速排序首先选取一个基准,一般选取第一个作为基准。然后选择两个哨兵`i``j`,对比大小,调整位置,使得左边的是小于基准的,右边的是大于基准的,然后将这两块分别递归处理。
119+
```C++
120+
```
121+
122+
# 插入排序
123+
把当前节点看作已排序好的节点,当前节点的索引为`j`,记录下一个节点的值为key,对下一个节点和当前节点的每一个进行比较,如果小于当前节点,和`j-1`节点进行比较,直至遇见小于或者到达`j==0`,插入到`j+1`的位置。
124+
```C++
125+
#include<iostream>
126+
#include<vector>
127+
using namespace std;
128+
129+
vector<int> insert_sort(vector<int>& num) {
130+
int size = num.size();
131+
if (size == 0) return num;
132+
133+
for (int j = 0; j < size - 1; j++) {
134+
int key = num[j + 1];
135+
while (j >= 0 && num[j] > key) {
136+
num[j + 1] = num[j];
137+
j--;
138+
}
139+
num[j + 1] = key;
140+
}
141+
return num;
142+
}
143+
144+
int main() {
145+
vector<int> num = { 6, 2, 1, 8, 2, 5, 0, 3, 4 };
146+
num = insert_sort(num);
147+
for (auto i : num)
148+
cout << i << " ";
149+
cout << endl;
150+
system("pause");
151+
return 0;
152+
}
113153
```

0 commit comments

Comments
 (0)