Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82a3f00

Browse files
committed
feat: update sort algorithms
1 parent 68df26c commit 82a3f00

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

‎basic/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
- 冒泡排序
66
- 插入排序
7+
- 归并排序

‎basic/sort/README.md‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class BubbleSort {
5656
与冒泡排序对比:
5757

5858
- 在冒泡排序中,经过每一轮的排序处理后,数组后端的数是排好序的。
59-
- 在插入排序中,经过每一轮的排序处理后,数组前端的数是拍好序的
59+
- 在插入排序中,经过每一轮的排序处理后,数组前端的数是排好序的
6060

6161
插入排序的算法思想是:不断将尚未排好序的数插入到已经排好序的部分。
6262

@@ -139,8 +139,9 @@ public class MergeSort {
139139
}
140140

141141
private static void mergeSort(int[] nums) {
142-
int[] temp = new int[nums.length];
143-
mergeSort(nums, 0, nums.length - 1, temp);
142+
int n = nums.length;
143+
int[] temp = new int[n];
144+
mergeSort(nums, 0, n - 1, temp);
144145
}
145146

146147
public static void main(String[] args) {
@@ -150,3 +151,17 @@ public class MergeSort {
150151
}
151152
}
152153
```
154+
155+
### 算法分析
156+
157+
空间复杂度 O(n),时间复杂度 O(nlogn)。
158+
159+
对于规模为 n 的问题,一共要进行 log(n) 次的切分,每一层的合并复杂度都是 O(n),所以整体时间复杂度为 O(nlogn)。
160+
161+
由于合并 n 个元素需要分配一个大小为 n 的额外数组,所以空间复杂度为 O(n)。
162+
163+
这是一种稳定的排序算法。
164+
165+
## 快速排序
166+
167+
快速排序也采用了分治的思想:把原始的数组筛选成较小和较大的两个子数组,然后递归地排序两个子数组。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /