mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Update the comments of bubble sort
and insertion sort
This commit is contained in:
@ -48,9 +48,9 @@ func quadratic(n int) int {
|
||||
/* 平方阶(冒泡排序) */
|
||||
func bubbleSort(nums []int) int {
|
||||
count := 0 // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for i := len(nums) - 1; i > 0; i-- {
|
||||
// 内循环:冒泡操作
|
||||
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
for j := 0; j < i; j++ {
|
||||
if nums[j] > nums[j+1] {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
|
||||
Reference in New Issue
Block a user