File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change 44
55- 冒泡排序
66- 插入排序
7+ - 归并排序
Original file line number Diff line number Diff 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+ 快速排序也采用了分治的思想:把原始的数组筛选成较小和较大的两个子数组,然后递归地排序两个子数组。
You can’t perform that action at this time.
0 commit comments