Skip to content

Commit b97957b

Browse files
authored
Update 1.bubbleSort.md
1 parent 9aff8bc commit b97957b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

1.bubbleSort.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,26 @@
2020

2121
![动图演示](res/bubbleSort.gif)
2222

23+
## 3. 算法分析
2324

24-
## 3. 什么时候最快
25+
#### 3.1 什么时候最快
2526

2627
当输入的数据已经是正序时(都已经是正序了,我还要你冒泡排序有何用啊)。
2728
这时只进行一趟排序:元素比较次数为n-1,元素移动为0。
2829

29-
30-
## 4. 什么时候最慢
30+
#### 3.2 什么时候最慢
3131

3232
当输入的数据是反序时(写一个 for 循环反序输出数据不就行了,干嘛要用你冒泡排序呢,我是闲的吗)。
3333

34+
#### 3.3 时间复杂度
35+
36+
设初始文件是反序的,需要进行 n-1 趟排序。每趟排序要进行 n-i 次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值:
37+
$$ 比较:C_{max} = \frac{n(n-1)}{2} = O(n^2)$$
38+
$$ 移动:M_{max} = \frac{3n(n-1)}{2} = O(n^2)$$
39+
40+
冒泡排序的最坏时间复杂度为$$ O(n^2)$$
41+
综上,因此冒泡排序总的平均时间复杂度为$$ O(n^2)$$
42+
3443

3544
## 5. JavaScript 代码实现
3645

0 commit comments

Comments
 (0)