Update the comments of bubble sort

and insertion sort
This commit is contained in:
krahets
2023-05-22 23:05:37 +08:00
parent 5b406666d8
commit f6d290d903
36 changed files with 119 additions and 119 deletions

View File

@ -53,11 +53,11 @@ fn quadratic(n: i32) i32 {
// 平方阶(冒泡排序)
fn bubbleSort(nums: []i32) i32 {
var count: i32 = 0; // 计数器
// 外循环:排序元素数量为 n-1, n-2, ..., 1
// 外循环:排序区间为 [0, i]
var i: i32 = @intCast(i32, nums.len ) - 1;
while (i > 0) : (i -= 1) {
var j: usize = 0;
// 内循环:冒泡操作
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
while (j < i) : (j += 1) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]