01. 冒泡排序 #30
Replies: 7 comments 3 replies
-
python中交换两个两个数可以不使用temp暂存 |
Beta Was this translation helpful? Give feedback.
-
然后将第 2 个元素与第 3 个元素比较,也就是将 2 和 3 进行比较。因为 2 < 3,所以不用交换; 这句话有笔误吧? 此时第二个元素是6 而不是2 |
Beta Was this translation helpful? Give feedback.
-
for i in range(len(arr)): 这里需要-1吧 |
Beta Was this translation helpful? Give feedback.
-
是的,需要减 1。已更正,感谢指正。 |
Beta Was this translation helpful? Give feedback.
-
代码实现没有最佳时间复杂度O(n)的优化,需要在第二个循环判断是否进行了位置交换 |
Beta Was this translation helpful? Give feedback.
-
def bubble_sort(arr): |
Beta Was this translation helpful? Give feedback.
-
01. 冒泡排序 | 算法通关手册
第 i (i = 1,2,… ) 趟排序时从序列中前 n - i + 1 个元素的第 1 个元素开始,相邻两个元素进行比较,若前者大于后者,两者交换位置,否则不交换。
冒泡排序
https://algo.itcharge.cn/01.Array/02.Array-Sort/01.Array-Bubble-Sort/
Beta Was this translation helpful? Give feedback.
All reactions