Skip to content

Commit 034ee65

Browse files
authored
Fix bugs and harmonize the code comments (#1199)
* Fix the comment in array_deque.go * Fix the comment in bucket_sort.c * Translate the Java code comments to Chinese * Bug fixes * 二分查找 -> 二分搜尋 * Harmonize comments in `utils` between multiple programming languages
1 parent cfe8281 commit 034ee65

File tree

35 files changed

+133
-271
lines changed

35 files changed

+133
-271
lines changed

codes/c/chapter_sorting/bucket_sort.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "../utils/common.h"
88

9-
#define ARRAY_SIZE 10
9+
#define SIZE 10
1010

1111
/* 比较两个浮点数的大小 */
1212
int compare_float(const void *a, const void *b) {
@@ -28,8 +28,8 @@ void bucketSort(float nums[], int size) {
2828
int k = size / 2;
2929
float **buckets = calloc(k, sizeof(float *));
3030
for (int i = 0; i < k; i++) {
31-
// 每个桶最多可以分配 k 个元素
32-
buckets[i] = calloc(ARRAY_SIZE, sizeof(float));
31+
// 每个桶最多可以分配 size 个元素
32+
buckets[i] = calloc(size, sizeof(float));
3333
}
3434

3535
// 1. 将数组元素分配到各个桶中
@@ -42,7 +42,7 @@ void bucketSort(float nums[], int size) {
4242
j++;
4343
}
4444
float temp = nums[i];
45-
while (j < ARRAY_SIZE && buckets[bucket_idx][j] > 0) {
45+
while (j < size && buckets[bucket_idx][j] > 0) {
4646
swap(&temp, &buckets[bucket_idx][j]);
4747
j++;
4848
}
@@ -51,12 +51,12 @@ void bucketSort(float nums[], int size) {
5151

5252
// 2. 对各个桶执行排序
5353
for (int i = 0; i < k; i++) {
54-
qsort(buckets[i], ARRAY_SIZE, sizeof(float), compare_float);
54+
qsort(buckets[i], size, sizeof(float), compare_float);
5555
}
5656

5757
// 3. 遍历桶合并结果
5858
for (int i = 0, j = 0; j < k; j++) {
59-
for (int l = 0; l < ARRAY_SIZE; l++) {
59+
for (int l = 0; l < size; l++) {
6060
if (buckets[j][l] > 0) {
6161
nums[i++] = buckets[j][l];
6262
}
@@ -73,10 +73,10 @@ void bucketSort(float nums[], int size) {
7373
/* Driver Code */
7474
int main() {
7575
// 设输入数据为浮点数,范围为 [0, 1)
76-
float nums[ARRAY_SIZE] = {0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f};
77-
bucketSort(nums, ARRAY_SIZE);
76+
float nums[SIZE] = {0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f};
77+
bucketSort(nums, SIZE);
7878
printf("桶排序完成后 nums = ");
79-
printArrayFloat(nums, ARRAY_SIZE);
79+
printArrayFloat(nums, SIZE);
8080

8181
return 0;
8282
}

codes/c/utils/common_test.c

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ void testListNode() {
1111
int size = sizeof(nums) / sizeof(int);
1212
ListNode *head = arrToLinkedList(nums, size);
1313
printLinkedList(head);
14-
15-
ListNode *node = getListNode(head, 5);
16-
printf("find node: %d\n", node->val);
1714
}
1815

1916
void testTreeNode() {

codes/c/utils/list_node.h

+2-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ListNode *newListNode(int val) {
2626
return node;
2727
}
2828

29-
/* Generate a linked list with an array */
29+
/* 将数组反序列化为链表 */
3030
ListNode *arrToLinkedList(const int *arr, size_t size) {
3131
if (size <= 0) {
3232
return NULL;
@@ -41,15 +41,7 @@ ListNode *arrToLinkedList(const int *arr, size_t size) {
4141
return dummy->next;
4242
}
4343

44-
/* Get a list node with specific value from a linked list */
45-
ListNode *getListNode(ListNode *head, int val) {
46-
while (head != NULL && head->val != val) {
47-
head = head->next;
48-
}
49-
return head;
50-
}
51-
52-
/* Free the memory allocated to a linked list */
44+
/* 释放分配给链表的内存空间 */
5345
void freeMemoryLinkedList(ListNode *cur) {
5446
// 释放内存
5547
ListNode *pre;

codes/c/utils/print_util.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
extern "C" {
1919
#endif
2020

21-
/* Print an Array */
21+
/* 打印数组 */
2222
void printArray(int arr[], int size) {
2323
if (arr == NULL || size == 0) {
2424
printf("[]");
@@ -31,7 +31,7 @@ void printArray(int arr[], int size) {
3131
printf("%d]\n", arr[size - 1]);
3232
}
3333

34-
/* Print an Array */
34+
/* 打印数组 */
3535
void printArrayFloat(float arr[], int size) {
3636
if (arr == NULL || size == 0) {
3737
printf("[]");
@@ -44,7 +44,7 @@ void printArrayFloat(float arr[], int size) {
4444
printf("%.2f]\n", arr[size - 1]);
4545
}
4646

47-
/* Print a linked list */
47+
/* 打印链表 */
4848
void printLinkedList(ListNode *node) {
4949
if (node == NULL) {
5050
return;
@@ -69,7 +69,6 @@ Trunk *newTrunk(Trunk *prev, char *str) {
6969
return trunk;
7070
}
7171

72-
/* Helper function to print branches of the binary tree */
7372
void showTrunks(Trunk *trunk) {
7473
if (trunk == NULL) {
7574
return;
@@ -78,7 +77,11 @@ void showTrunks(Trunk *trunk) {
7877
printf("%s", trunk->str);
7978
}
8079

81-
/* Help to print a binary tree, hide more details */
80+
/**
81+
* 打印二叉树
82+
* This tree printer is borrowed from TECHIE DELIGHT
83+
* https://www.techiedelight.com/c-program-print-binary-tree/
84+
*/
8285
void printTreeHelper(TreeNode *node, Trunk *prev, bool isRight) {
8386
if (node == NULL) {
8487
return;
@@ -106,12 +109,12 @@ void printTreeHelper(TreeNode *node, Trunk *prev, bool isRight) {
106109
printTreeHelper(node->left, trunk, false);
107110
}
108111

109-
/* Print a binary tree */
112+
/* 打印二叉树 */
110113
void printTree(TreeNode *root) {
111114
printTreeHelper(root, NULL, false);
112115
}
113116

114-
/* Print a Heap */
117+
/* 打印堆 */
115118
void printHeap(int arr[], int size) {
116119
TreeNode *root;
117120
printf("堆的数组表示:");

codes/cpp/utils/list_node.hpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
using namespace std;
1313

14-
/* Definition for a singly-linked list node */
14+
/* 链表节点 */
1515
struct ListNode {
1616
int val;
1717
ListNode *next;
1818
ListNode(int x) : val(x), next(nullptr) {
1919
}
2020
};
2121

22-
/* Generate a linked list with a vector */
22+
/* 将列表反序列化为链表 */
2323
ListNode *vecToLinkedList(vector<int> list) {
2424
ListNode *dum = new ListNode(0);
2525
ListNode *head = dum;
@@ -30,15 +30,7 @@ ListNode *vecToLinkedList(vector<int> list) {
3030
return dum->next;
3131
}
3232

33-
/* Get a list node with specific value from a linked list */
34-
ListNode *getListNode(ListNode *head, int val) {
35-
while (head != nullptr && head->val != val) {
36-
head = head->next;
37-
}
38-
return head;
39-
}
40-
41-
/* Free the memory allocated to a linked list */
33+
/* 释放分配给链表的内存空间 */
4234
void freeMemoryLinkedList(ListNode *cur) {
4335
// 释放内存
4436
ListNode *pre;

codes/cpp/utils/print_utils.hpp

+15-16
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ string strRepeat(string str, int n) {
4444
return os.str();
4545
}
4646

47-
/* Print an Array */
47+
/* 打印数组 */
4848
template <typename T> void printArray(T *arr, int n) {
4949
cout << "[";
5050
for (int i = 0; i < n - 1; i++) {
@@ -61,20 +61,20 @@ template <typename T> string getVectorString(vector<T> &list) {
6161
return "[" + strJoin(", ", list) + "]";
6262
}
6363

64-
/* Print a vector */
64+
/* 打印列表 */
6565
template <typename T> void printVector(vector<T> list) {
6666
cout << getVectorString(list) << '\n';
6767
}
6868

69-
/* Print a vector matrix */
69+
/* 打印矩阵 */
7070
template <typename T> void printVectorMatrix(vector<vector<T>> &matrix) {
7171
cout << "[" << '\n';
7272
for (vector<T> &list : matrix)
7373
cout << " " + getVectorString(list) + "," << '\n';
7474
cout << "]" << '\n';
7575
}
7676

77-
/* Print a linked list */
77+
/* 打印链表 */
7878
void printLinkedList(ListNode *head) {
7979
vector<int> list;
8080
while (head != nullptr) {
@@ -85,10 +85,6 @@ void printLinkedList(ListNode *head) {
8585
cout << strJoin(" -> ", list) << '\n';
8686
}
8787

88-
/**
89-
* This tree printer is borrowed from TECHIE DELIGHT
90-
* https://www.techiedelight.com/c-program-print-binary-tree/
91-
*/
9288
struct Trunk {
9389
Trunk *prev;
9490
string str;
@@ -98,7 +94,6 @@ struct Trunk {
9894
}
9995
};
10096

101-
/* Helper function to print branches of the binary tree */
10297
void showTrunks(Trunk *p) {
10398
if (p == nullptr) {
10499
return;
@@ -108,7 +103,11 @@ void showTrunks(Trunk *p) {
108103
cout << p->str;
109104
}
110105

111-
/* Print a binary tree */
106+
/**
107+
* 打印二叉树
108+
* This tree printer is borrowed from TECHIE DELIGHT
109+
* https://www.techiedelight.com/c-program-print-binary-tree/
110+
*/
112111
void printTree(TreeNode *root, Trunk *prev, bool isRight) {
113112
if (root == nullptr) {
114113
return;
@@ -140,12 +139,12 @@ void printTree(TreeNode *root, Trunk *prev, bool isRight) {
140139
printTree(root->left, &trunk, false);
141140
}
142141

143-
/* The interface of the tree printer */
142+
/* 打印二叉树 */
144143
void printTree(TreeNode *root) {
145144
printTree(root, nullptr, false);
146145
}
147146

148-
/* Print a stack */
147+
/* 打印栈 */
149148
template <typename T> void printStack(stack<T> stk) {
150149
// Reverse the input stack
151150
stack<T> tmp;
@@ -167,7 +166,7 @@ template <typename T> void printStack(stack<T> stk) {
167166
cout << "[" + s.str() + "]" << '\n';
168167
}
169168

170-
/* Print a queue */
169+
/* 打印队列 */
171170
template <typename T> void printQueue(queue<T> queue) {
172171
// Generate the string to print
173172
ostringstream s;
@@ -183,7 +182,7 @@ template <typename T> void printQueue(queue<T> queue) {
183182
cout << "[" + s.str() + "]" << '\n';
184183
}
185184

186-
/* Print a deque */
185+
/* 打印双向队列 */
187186
template <typename T> void printDeque(deque<T> deque) {
188187
// Generate the string to print
189188
ostringstream s;
@@ -199,7 +198,7 @@ template <typename T> void printDeque(deque<T> deque) {
199198
cout << "[" + s.str() + "]" << '\n';
200199
}
201200

202-
/* Print a HashMap */
201+
/* 打印哈希表 */
203202
// 定义模板参数 TKey 和 TValue ,用于指定键值对的类型
204203
template <typename TKey, typename TValue> void printHashMap(unordered_map<TKey, TValue> map) {
205204
for (auto kv : map) {
@@ -217,7 +216,7 @@ template <typename T, typename S, typename C> S &Container(priority_queue<T, S,
217216
return HackedQueue::Container(pq);
218217
}
219218

220-
/* Print a Heap (PriorityQueue) */
219+
/* 打印堆(优先队列) */
221220
template <typename T, typename S, typename C> void printHeap(priority_queue<T, S, C> &heap) {
222221
vector<T> vec = Container(heap);
223222
cout << "堆的数组表示:";

codes/csharp/utils/ListNode.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace hello_algo.utils;
66

7-
/* Definition for a singly-linked list node */
7+
/* 链表节点 */
88
public class ListNode(int x) {
99
public int val = x;
1010
public ListNode? next;
1111

12-
/* Generate a linked list with an array */
12+
/* 将数组反序列化为链表 */
1313
public static ListNode? ArrToLinkedList(int[] arr) {
1414
ListNode dum = new(0);
1515
ListNode head = dum;
@@ -20,14 +20,6 @@ public class ListNode(int x) {
2020
return dum.next;
2121
}
2222

23-
/* Get a list node with specific value from a linked list */
24-
public static ListNode? GetListNode(ListNode? head, int val) {
25-
while (head != null && head.val != val) {
26-
head = head.next;
27-
}
28-
return head;
29-
}
30-
3123
public override string? ToString() {
3224
List<string> list = [];
3325
var head = this;

0 commit comments

Comments
 (0)