From f6d290d903e157298b5ff2f73fc248ae1867e774 Mon Sep 17 00:00:00 2001 From: krahets Date: Mon, 22 May 2023 23:05:37 +0800 Subject: [PATCH] Update the comments of bubble sort and insertion sort --- .../c/chapter_computational_complexity/time_complexity.c | 4 ++-- codes/c/chapter_sorting/bubble_sort.c | 8 ++++---- codes/c/chapter_sorting/insertion_sort.c | 8 ++++---- .../chapter_computational_complexity/time_complexity.cpp | 4 ++-- codes/cpp/chapter_sorting/bubble_sort.cpp | 8 ++++---- codes/cpp/chapter_sorting/insertion_sort.cpp | 8 ++++---- .../chapter_computational_complexity/time_complexity.cs | 4 ++-- codes/csharp/chapter_sorting/bubble_sort.cs | 8 ++++---- codes/csharp/chapter_sorting/insertion_sort.cs | 8 ++++---- .../chapter_computational_complexity/time_complexity.dart | 4 ++-- codes/dart/chapter_sorting/bubble_sort.dart | 8 ++++---- codes/dart/chapter_sorting/insertion_sort.dart | 8 ++++---- .../chapter_computational_complexity/time_complexity.go | 4 ++-- codes/go/chapter_sorting/bubble_sort.go | 8 ++++---- codes/go/chapter_sorting/insertion_sort.go | 8 ++++---- .../chapter_computational_complexity/time_complexity.java | 4 ++-- codes/java/chapter_sorting/bubble_sort.java | 8 ++++---- codes/java/chapter_sorting/insertion_sort.java | 8 ++++---- .../chapter_computational_complexity/time_complexity.js | 4 ++-- codes/javascript/chapter_sorting/bubble_sort.js | 8 ++++---- codes/javascript/chapter_sorting/insertion_sort.js | 8 ++++---- .../chapter_computational_complexity/time_complexity.py | 4 ++-- codes/python/chapter_sorting/bubble_sort.py | 8 ++++---- codes/python/chapter_sorting/insertion_sort.py | 8 ++++---- .../chapter_computational_complexity/time_complexity.rs | 4 ++-- codes/rust/chapter_sorting/bubble_sort.rs | 8 ++++---- codes/rust/chapter_sorting/insertion_sort.rs | 8 ++++---- .../time_complexity.swift | 4 ++-- codes/swift/chapter_sorting/bubble_sort.swift | 6 +++--- codes/swift/chapter_sorting/insertion_sort.swift | 8 ++++---- .../chapter_computational_complexity/time_complexity.ts | 4 ++-- codes/typescript/chapter_sorting/bubble_sort.ts | 8 ++++---- codes/typescript/chapter_sorting/insertion_sort.ts | 8 ++++---- .../chapter_computational_complexity/time_complexity.zig | 4 ++-- codes/zig/chapter_sorting/bubble_sort.zig | 8 ++++---- codes/zig/chapter_sorting/insertion_sort.zig | 8 ++++---- 36 files changed, 119 insertions(+), 119 deletions(-) diff --git a/codes/c/chapter_computational_complexity/time_complexity.c b/codes/c/chapter_computational_complexity/time_complexity.c index c9a2fd2d..e31a86fc 100644 --- a/codes/c/chapter_computational_complexity/time_complexity.c +++ b/codes/c/chapter_computational_complexity/time_complexity.c @@ -51,9 +51,9 @@ int quadratic(int n) { /* 平方阶(冒泡排序) */ int bubbleSort(int *nums, int n) { int count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = n - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/c/chapter_sorting/bubble_sort.c b/codes/c/chapter_sorting/bubble_sort.c index 59181d3e..bebd41fb 100644 --- a/codes/c/chapter_sorting/bubble_sort.c +++ b/codes/c/chapter_sorting/bubble_sort.c @@ -8,9 +8,9 @@ /* 冒泡排序 */ void bubbleSort(int nums[], int size) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = 0; i < size - 1; i++) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < size - 1 - i; j++) { if (nums[j] > nums[j + 1]) { int temp = nums[j]; @@ -23,10 +23,10 @@ void bubbleSort(int nums[], int size) { /* 冒泡排序(标志优化)*/ void bubbleSortWithFlag(int nums[], int size) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = 0; i < size - 1; i++) { bool flag = false; - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < size - 1 - i; j++) { if (nums[j] > nums[j + 1]) { int temp = nums[j]; diff --git a/codes/c/chapter_sorting/insertion_sort.c b/codes/c/chapter_sorting/insertion_sort.c index 32ca1e9b..56171a93 100644 --- a/codes/c/chapter_sorting/insertion_sort.c +++ b/codes/c/chapter_sorting/insertion_sort.c @@ -8,16 +8,16 @@ /* 插入排序 */ void insertionSort(int nums[], int size) { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for (int i = 1; i < size; i++) { int base = nums[i], j = i - 1; - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while (j >= 0 && nums[j] > base) { - // 1. 将 nums[j] 向右移动一位 + // 将 nums[j] 向右移动一位 nums[j + 1] = nums[j]; j--; } - // 2. 将 base 赋值到正确位置 + // 将 base 赋值到正确位置 nums[j + 1] = base; } } diff --git a/codes/cpp/chapter_computational_complexity/time_complexity.cpp b/codes/cpp/chapter_computational_complexity/time_complexity.cpp index 253605ce..002f3ab0 100644 --- a/codes/cpp/chapter_computational_complexity/time_complexity.cpp +++ b/codes/cpp/chapter_computational_complexity/time_complexity.cpp @@ -48,9 +48,9 @@ int quadratic(int n) { /* 平方阶(冒泡排序) */ int bubbleSort(vector &nums) { int count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.size() - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/cpp/chapter_sorting/bubble_sort.cpp b/codes/cpp/chapter_sorting/bubble_sort.cpp index 9e268085..21378672 100644 --- a/codes/cpp/chapter_sorting/bubble_sort.cpp +++ b/codes/cpp/chapter_sorting/bubble_sort.cpp @@ -8,9 +8,9 @@ /* 冒泡排序 */ void bubbleSort(vector &nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.size() - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -23,10 +23,10 @@ void bubbleSort(vector &nums) { /* 冒泡排序(标志优化)*/ void bubbleSortWithFlag(vector &nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.size() - 1; i > 0; i--) { bool flag = false; // 初始化标志位 - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/cpp/chapter_sorting/insertion_sort.cpp b/codes/cpp/chapter_sorting/insertion_sort.cpp index 1d37acb8..62c58543 100644 --- a/codes/cpp/chapter_sorting/insertion_sort.cpp +++ b/codes/cpp/chapter_sorting/insertion_sort.cpp @@ -8,15 +8,15 @@ /* 插入排序 */ void insertionSort(vector &nums) { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for (int i = 1; i < nums.size(); i++) { int base = nums[i], j = i - 1; - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while (j >= 0 && nums[j] > base) { - nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j]; // 将 nums[j] 向右移动一位 j--; } - nums[j + 1] = base; // 2. 将 base 赋值到正确位置 + nums[j + 1] = base; // 将 base 赋值到正确位置 } } diff --git a/codes/csharp/chapter_computational_complexity/time_complexity.cs b/codes/csharp/chapter_computational_complexity/time_complexity.cs index d930cc0b..3e7c68e1 100644 --- a/codes/csharp/chapter_computational_complexity/time_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/time_complexity.cs @@ -81,9 +81,9 @@ public class time_complexity { /* 平方阶(冒泡排序) */ static int bubbleSort(int[] nums) { int count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.Length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/csharp/chapter_sorting/bubble_sort.cs b/codes/csharp/chapter_sorting/bubble_sort.cs index 6e4d0c25..1044c5cf 100644 --- a/codes/csharp/chapter_sorting/bubble_sort.cs +++ b/codes/csharp/chapter_sorting/bubble_sort.cs @@ -9,9 +9,9 @@ namespace hello_algo.chapter_sorting; public class bubble_sort { /* 冒泡排序 */ static void bubbleSort(int[] nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.Length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -25,10 +25,10 @@ public class bubble_sort { /* 冒泡排序(标志优化)*/ static void bubbleSortWithFlag(int[] nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.Length - 1; i > 0; i--) { bool flag = false; // 初始化标志位 - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/csharp/chapter_sorting/insertion_sort.cs b/codes/csharp/chapter_sorting/insertion_sort.cs index a6a50982..45a195cd 100644 --- a/codes/csharp/chapter_sorting/insertion_sort.cs +++ b/codes/csharp/chapter_sorting/insertion_sort.cs @@ -9,15 +9,15 @@ namespace hello_algo.chapter_sorting; public class insertion_sort { /* 插入排序 */ static void insertionSort(int[] nums) { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for (int i = 1; i < nums.Length; i++) { int bas = nums[i], j = i - 1; - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while (j >= 0 && nums[j] > bas) { - nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j]; // 将 nums[j] 向右移动一位 j--; } - nums[j + 1] = bas; // 2. 将 base 赋值到正确位置 + nums[j + 1] = bas; // 将 base 赋值到正确位置 } } diff --git a/codes/dart/chapter_computational_complexity/time_complexity.dart b/codes/dart/chapter_computational_complexity/time_complexity.dart index b0cef616..e8fc9e92 100644 --- a/codes/dart/chapter_computational_complexity/time_complexity.dart +++ b/codes/dart/chapter_computational_complexity/time_complexity.dart @@ -48,9 +48,9 @@ int quadratic(int n) { /* 平方阶(冒泡排序) */ int bubbleSort(List nums) { int count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (var i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (var j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/dart/chapter_sorting/bubble_sort.dart b/codes/dart/chapter_sorting/bubble_sort.dart index ae02dce5..b563ddf6 100644 --- a/codes/dart/chapter_sorting/bubble_sort.dart +++ b/codes/dart/chapter_sorting/bubble_sort.dart @@ -6,9 +6,9 @@ /* 冒泡排序 */ void bubbleSort(List nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -22,10 +22,10 @@ void bubbleSort(List nums) { /* 冒泡排序(标志优化)*/ void bubbleSortWithFlag(List nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.length - 1; i > 0; i--) { bool flag = false; // 初始化标志位 - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/dart/chapter_sorting/insertion_sort.dart b/codes/dart/chapter_sorting/insertion_sort.dart index aaa4f22d..ffc1cc95 100644 --- a/codes/dart/chapter_sorting/insertion_sort.dart +++ b/codes/dart/chapter_sorting/insertion_sort.dart @@ -6,15 +6,15 @@ /* 插入排序 */ void insertionSort(List nums) { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for (int i = 1; i < nums.length; i++) { int base = nums[i], j = i - 1; - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while (j >= 0 && nums[j] > base) { - nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j]; // 将 nums[j] 向右移动一位 j--; } - nums[j + 1] = base; // 2. 将 base 赋值到正确位置 + nums[j + 1] = base; // 将 base 赋值到正确位置 } } diff --git a/codes/go/chapter_computational_complexity/time_complexity.go b/codes/go/chapter_computational_complexity/time_complexity.go index 15bb8f25..caf327ed 100644 --- a/codes/go/chapter_computational_complexity/time_complexity.go +++ b/codes/go/chapter_computational_complexity/time_complexity.go @@ -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] diff --git a/codes/go/chapter_sorting/bubble_sort.go b/codes/go/chapter_sorting/bubble_sort.go index ff1ff9ee..66167a4c 100644 --- a/codes/go/chapter_sorting/bubble_sort.go +++ b/codes/go/chapter_sorting/bubble_sort.go @@ -6,9 +6,9 @@ package chapter_sorting /* 冒泡排序 */ func bubbleSort(nums []int) { - // 外循环:待排序元素数量为 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] @@ -20,10 +20,10 @@ func bubbleSort(nums []int) { /* 冒泡排序(标志优化)*/ func bubbleSortWithFlag(nums []int) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for i := len(nums) - 1; i > 0; i-- { flag := false // 初始化标志位 - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j := 0; j < i; j++ { if nums[j] > nums[j+1] { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/go/chapter_sorting/insertion_sort.go b/codes/go/chapter_sorting/insertion_sort.go index 49c28005..30c9e452 100644 --- a/codes/go/chapter_sorting/insertion_sort.go +++ b/codes/go/chapter_sorting/insertion_sort.go @@ -6,15 +6,15 @@ package chapter_sorting /* 插入排序 */ func insertionSort(nums []int) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for i := 1; i < len(nums); i++ { base := nums[i] j := i - 1 - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 for j >= 0 && nums[j] > base { - nums[j+1] = nums[j] // 1. 将 nums[j] 向右移动一位 + nums[j+1] = nums[j] // 将 nums[j] 向右移动一位 j-- } - nums[j+1] = base // 2. 将 base 赋值到正确位置 + nums[j+1] = base // 将 base 赋值到正确位置 } } diff --git a/codes/java/chapter_computational_complexity/time_complexity.java b/codes/java/chapter_computational_complexity/time_complexity.java index ff3303db..802e791a 100644 --- a/codes/java/chapter_computational_complexity/time_complexity.java +++ b/codes/java/chapter_computational_complexity/time_complexity.java @@ -49,9 +49,9 @@ public class time_complexity { /* 平方阶(冒泡排序) */ static int bubbleSort(int[] nums) { int count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/java/chapter_sorting/bubble_sort.java b/codes/java/chapter_sorting/bubble_sort.java index 3eb2c5b7..ca03a9c1 100644 --- a/codes/java/chapter_sorting/bubble_sort.java +++ b/codes/java/chapter_sorting/bubble_sort.java @@ -11,9 +11,9 @@ import java.util.*; public class bubble_sort { /* 冒泡排序 */ static void bubbleSort(int[] nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -27,10 +27,10 @@ public class bubble_sort { /* 冒泡排序(标志优化) */ static void bubbleSortWithFlag(int[] nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (int i = nums.length - 1; i > 0; i--) { boolean flag = false; // 初始化标志位 - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/java/chapter_sorting/insertion_sort.java b/codes/java/chapter_sorting/insertion_sort.java index e79610fa..a79e1d54 100644 --- a/codes/java/chapter_sorting/insertion_sort.java +++ b/codes/java/chapter_sorting/insertion_sort.java @@ -11,15 +11,15 @@ import java.util.*; public class insertion_sort { /* 插入排序 */ static void insertionSort(int[] nums) { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for (int i = 1; i < nums.length; i++) { int base = nums[i], j = i - 1; - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while (j >= 0 && nums[j] > base) { - nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j]; // 将 nums[j] 向右移动一位 j--; } - nums[j + 1] = base; // 2. 将 base 赋值到正确位置 + nums[j + 1] = base; // 将 base 赋值到正确位置 } } diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 235dc7f4..78c12436 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -44,9 +44,9 @@ function quadratic(n) { /* 平方阶(冒泡排序) */ function bubbleSort(nums) { let count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/javascript/chapter_sorting/bubble_sort.js b/codes/javascript/chapter_sorting/bubble_sort.js index 716f5b62..77854169 100644 --- a/codes/javascript/chapter_sorting/bubble_sort.js +++ b/codes/javascript/chapter_sorting/bubble_sort.js @@ -6,9 +6,9 @@ /* 冒泡排序 */ function bubbleSort(nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -22,10 +22,10 @@ function bubbleSort(nums) { /* 冒泡排序(标志优化)*/ function bubbleSortWithFlag(nums) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { let flag = false; // 初始化标志位 - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/javascript/chapter_sorting/insertion_sort.js b/codes/javascript/chapter_sorting/insertion_sort.js index f004f380..d2e6f223 100644 --- a/codes/javascript/chapter_sorting/insertion_sort.js +++ b/codes/javascript/chapter_sorting/insertion_sort.js @@ -6,16 +6,16 @@ /* 插入排序 */ function insertionSort(nums) { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for (let i = 1; i < nums.length; i++) { let base = nums[i], j = i - 1; - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while (j >= 0 && nums[j] > base) { - nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j]; // 将 nums[j] 向右移动一位 j--; } - nums[j + 1] = base; // 2. 将 base 赋值到正确位置 + nums[j + 1] = base; // 将 base 赋值到正确位置 } } diff --git a/codes/python/chapter_computational_complexity/time_complexity.py b/codes/python/chapter_computational_complexity/time_complexity.py index af728361..a3da3f4e 100644 --- a/codes/python/chapter_computational_complexity/time_complexity.py +++ b/codes/python/chapter_computational_complexity/time_complexity.py @@ -44,9 +44,9 @@ def quadratic(n: int) -> int: def bubble_sort(nums: list[int]) -> int: """平方阶(冒泡排序)""" count = 0 # 计数器 - # 外循环:待排序元素数量为 n-1, n-2, ..., 1 + # 外循环:未排序区间为 [0, i] for i in range(len(nums) - 1, 0, -1): - # 内循环:冒泡操作 + # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in range(i): if nums[j] > nums[j + 1]: # 交换 nums[j] 与 nums[j + 1] diff --git a/codes/python/chapter_sorting/bubble_sort.py b/codes/python/chapter_sorting/bubble_sort.py index 5d062dbc..5218db3d 100644 --- a/codes/python/chapter_sorting/bubble_sort.py +++ b/codes/python/chapter_sorting/bubble_sort.py @@ -8,9 +8,9 @@ Author: timi (xisunyy@163.com) def bubble_sort(nums: list[int]) -> None: """冒泡排序""" n = len(nums) - # 外循环:待排序元素数量为 n-1, n-2, ..., 1 + # 外循环:未排序区间为 [0, i] for i in range(n - 1, 0, -1): - # 内循环:冒泡操作 + # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in range(i): if nums[j] > nums[j + 1]: # 交换 nums[j] 与 nums[j + 1] @@ -20,10 +20,10 @@ def bubble_sort(nums: list[int]) -> None: def bubble_sort_with_flag(nums: list[int]) -> None: """冒泡排序(标志优化)""" n = len(nums) - # 外循环:待排序元素数量为 n-1, n-2, ..., 1 + # 外循环:未排序区间为 [0, i] for i in range(n - 1, 0, -1): flag = False # 初始化标志位 - # 内循环:冒泡操作 + # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in range(i): if nums[j] > nums[j + 1]: # 交换 nums[j] 与 nums[j + 1] diff --git a/codes/python/chapter_sorting/insertion_sort.py b/codes/python/chapter_sorting/insertion_sort.py index 6c5423b9..fdaf26fd 100644 --- a/codes/python/chapter_sorting/insertion_sort.py +++ b/codes/python/chapter_sorting/insertion_sort.py @@ -7,15 +7,15 @@ Author: timi (xisunyy@163.com) def insertion_sort(nums: list[int]) -> None: """插入排序""" - # 外循环:base = nums[1], nums[2], ..., nums[n-1] + # 外循环:已排序区间为 [0, i-1] for i in range(1, len(nums)): base = nums[i] j = i - 1 - # 内循环:将 base 插入到左边的正确位置 + # 内循环:将 base 插入到已排序区间 [0, i-1] 中的正确位置 while j >= 0 and nums[j] > base: - nums[j + 1] = nums[j] # 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j] # 将 nums[j] 向右移动一位 j -= 1 - nums[j + 1] = base # 2. 将 base 赋值到正确位置 + nums[j + 1] = base # 将 base 赋值到正确位置 """Driver Code""" diff --git a/codes/rust/chapter_computational_complexity/time_complexity.rs b/codes/rust/chapter_computational_complexity/time_complexity.rs index f7c2b6b0..0795a223 100644 --- a/codes/rust/chapter_computational_complexity/time_complexity.rs +++ b/codes/rust/chapter_computational_complexity/time_complexity.rs @@ -49,9 +49,9 @@ fn quadratic(n: i32) -> i32 { /* 平方阶(冒泡排序) */ fn bubble_sort(nums: &mut [i32]) -> i32 { let mut count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for i in (1..nums.len()).rev() { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in 0..i { if nums[j] > nums[j + 1] { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/rust/chapter_sorting/bubble_sort.rs b/codes/rust/chapter_sorting/bubble_sort.rs index b4957f7e..d0afc20a 100644 --- a/codes/rust/chapter_sorting/bubble_sort.rs +++ b/codes/rust/chapter_sorting/bubble_sort.rs @@ -8,9 +8,9 @@ include!("../include/include.rs"); /* 冒泡排序 */ fn bubble_sort(nums: &mut [i32]) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for i in (1..nums.len()).rev() { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in 0..i { if nums[j] > nums[j + 1] { // 交换 nums[j] 与 nums[j + 1] @@ -24,10 +24,10 @@ fn bubble_sort(nums: &mut [i32]) { /* 冒泡排序(标志优化) */ fn bubble_sort_with_flag(nums: &mut [i32]) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for i in (1..nums.len()).rev() { let mut flag = false; // 初始化标志位 - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in 0..i { if nums[j] > nums[j + 1] { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/rust/chapter_sorting/insertion_sort.rs b/codes/rust/chapter_sorting/insertion_sort.rs index af8bd262..a8354731 100644 --- a/codes/rust/chapter_sorting/insertion_sort.rs +++ b/codes/rust/chapter_sorting/insertion_sort.rs @@ -8,15 +8,15 @@ include!("../include/include.rs"); /*插入排序 */ fn insertion_sort(nums: &mut [i32]) { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for i in 1..nums.len() { let (base, mut j) = (nums[i], (i - 1) as i32); - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while j >= 0 && nums[j as usize] > base { - nums[(j + 1) as usize] = nums[j as usize]; // 1. 将 nums[j] 向右移动一位 + nums[(j + 1) as usize] = nums[j as usize]; // 将 nums[j] 向右移动一位 j -= 1; } - nums[(j + 1) as usize] = base; // 2. 将 base 赋值到正确位置 + nums[(j + 1) as usize] = base; // 将 base 赋值到正确位置 } } diff --git a/codes/swift/chapter_computational_complexity/time_complexity.swift b/codes/swift/chapter_computational_complexity/time_complexity.swift index 041ea213..04d0b349 100644 --- a/codes/swift/chapter_computational_complexity/time_complexity.swift +++ b/codes/swift/chapter_computational_complexity/time_complexity.swift @@ -48,9 +48,9 @@ func quadratic(n: Int) -> Int { /* 平方阶(冒泡排序) */ func bubbleSort(nums: inout [Int]) -> Int { var count = 0 // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for i in stride(from: nums.count - 1, to: 0, by: -1) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in 0 ..< i { if nums[j] > nums[j + 1] { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/swift/chapter_sorting/bubble_sort.swift b/codes/swift/chapter_sorting/bubble_sort.swift index 06409c3d..e065869f 100644 --- a/codes/swift/chapter_sorting/bubble_sort.swift +++ b/codes/swift/chapter_sorting/bubble_sort.swift @@ -6,9 +6,9 @@ /* 冒泡排序 */ func bubbleSort(nums: inout [Int]) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for i in stride(from: nums.count - 1, to: 0, by: -1) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for j in stride(from: 0, to: i, by: 1) { if nums[j] > nums[j + 1] { // 交换 nums[j] 与 nums[j + 1] @@ -22,7 +22,7 @@ func bubbleSort(nums: inout [Int]) { /* 冒泡排序(标志优化)*/ func bubbleSortWithFlag(nums: inout [Int]) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for i in stride(from: nums.count - 1, to: 0, by: -1) { var flag = false // 初始化标志位 for j in stride(from: 0, to: i, by: 1) { diff --git a/codes/swift/chapter_sorting/insertion_sort.swift b/codes/swift/chapter_sorting/insertion_sort.swift index d60228e8..4e665dc2 100644 --- a/codes/swift/chapter_sorting/insertion_sort.swift +++ b/codes/swift/chapter_sorting/insertion_sort.swift @@ -6,16 +6,16 @@ /* 插入排序 */ func insertionSort(nums: inout [Int]) { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for i in stride(from: 1, to: nums.count, by: 1) { let base = nums[i] var j = i - 1 - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while j >= 0, nums[j] > base { - nums[j + 1] = nums[j] // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j] // 将 nums[j] 向右移动一位 j -= 1 } - nums[j + 1] = base // 2. 将 base 赋值到正确位置 + nums[j + 1] = base // 将 base 赋值到正确位置 } } diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index 59902eb3..dfd4d2c5 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -44,9 +44,9 @@ function quadratic(n: number): number { /* 平方阶(冒泡排序) */ function bubbleSort(nums: number[]): number { let count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/typescript/chapter_sorting/bubble_sort.ts b/codes/typescript/chapter_sorting/bubble_sort.ts index 7ba4adc7..db7d0b56 100644 --- a/codes/typescript/chapter_sorting/bubble_sort.ts +++ b/codes/typescript/chapter_sorting/bubble_sort.ts @@ -6,9 +6,9 @@ /* 冒泡排序 */ function bubbleSort(nums: number[]): void { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] @@ -22,10 +22,10 @@ function bubbleSort(nums: number[]): void { /* 冒泡排序(标志优化)*/ function bubbleSortWithFlag(nums: number[]): void { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] for (let i = nums.length - 1; i > 0; i--) { let flag = false; // 初始化标志位 - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 for (let j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/typescript/chapter_sorting/insertion_sort.ts b/codes/typescript/chapter_sorting/insertion_sort.ts index 22852b20..d7e9e7c7 100644 --- a/codes/typescript/chapter_sorting/insertion_sort.ts +++ b/codes/typescript/chapter_sorting/insertion_sort.ts @@ -6,16 +6,16 @@ /* 插入排序 */ function insertionSort(nums: number[]): void { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n for (let i = 1; i < nums.length; i++) { const base = nums[i]; let j = i - 1; - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while (j >= 0 && nums[j] > base) { - nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j]; // 将 nums[j] 向右移动一位 j--; } - nums[j + 1] = base; // 2. 将 base 赋值到正确位置 + nums[j + 1] = base; // 将 base 赋值到正确位置 } } diff --git a/codes/zig/chapter_computational_complexity/time_complexity.zig b/codes/zig/chapter_computational_complexity/time_complexity.zig index 564bb7b7..8f49ea49 100644 --- a/codes/zig/chapter_computational_complexity/time_complexity.zig +++ b/codes/zig/chapter_computational_complexity/time_complexity.zig @@ -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] diff --git a/codes/zig/chapter_sorting/bubble_sort.zig b/codes/zig/chapter_sorting/bubble_sort.zig index 3a2a922e..ef4bf066 100644 --- a/codes/zig/chapter_sorting/bubble_sort.zig +++ b/codes/zig/chapter_sorting/bubble_sort.zig @@ -7,11 +7,11 @@ const inc = @import("include"); // 冒泡排序 fn bubbleSort(nums: []i32) void { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] var i: usize = 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] @@ -25,12 +25,12 @@ fn bubbleSort(nums: []i32) void { // 冒泡排序(标志优化) fn bubbleSortWithFlag(nums: []i32) void { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + // 外循环:未排序区间为 [0, i] var i: usize = nums.len - 1; while (i > 0) : (i -= 1) { var flag = false; // 初始化标志位 var j: usize = 0; - // 内循环:冒泡操作 + // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 while (j < i) : (j += 1) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] diff --git a/codes/zig/chapter_sorting/insertion_sort.zig b/codes/zig/chapter_sorting/insertion_sort.zig index b0f0fa84..f9fec703 100644 --- a/codes/zig/chapter_sorting/insertion_sort.zig +++ b/codes/zig/chapter_sorting/insertion_sort.zig @@ -7,16 +7,16 @@ const inc = @import("include"); // 插入排序 fn insertionSort(nums: []i32) void { - // 外循环:base = nums[1], nums[2], ..., nums[n-1] + // 外循环:已排序元素数量为 1, 2, ..., n var i: usize = 1; while (i < nums.len) : (i += 1) { var base = nums[i]; var j: usize = i; - // 内循环:将 base 插入到左边的正确位置 + // 内循环:将 base 插入到已排序部分的正确位置 while (j >= 1 and nums[j - 1] > base) : (j -= 1) { - nums[j] = nums[j - 1]; // 1. 将 nums[j] 向右移动一位 + nums[j] = nums[j - 1]; // 将 nums[j] 向右移动一位 } - nums[j] = base; // 2. 将 base 赋值到正确位置 + nums[j] = base; // 将 base 赋值到正确位置 } }