Skip to content

Commit

Permalink
Fix bugs and harmonize the code comments (#1199)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
krahets committed Mar 30, 2024
1 parent cfe8281 commit 034ee65
Show file tree
Hide file tree
Showing 35 changed files with 133 additions and 271 deletions.
18 changes: 9 additions & 9 deletions codes/c/chapter_sorting/bucket_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

#define ARRAY_SIZE 10
#define SIZE 10

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

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

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

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

return 0;
}
3 changes: 0 additions & 3 deletions codes/c/utils/common_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ void testListNode() {
int size = sizeof(nums) / sizeof(int);
ListNode *head = arrToLinkedList(nums, size);
printLinkedList(head);

ListNode *node = getListNode(head, 5);
printf("find node: %d\n", node->val);
}

void testTreeNode() {
Expand Down
12 changes: 2 additions & 10 deletions codes/c/utils/list_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ListNode *newListNode(int val) {
return node;
}

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

/* Get a list node with specific value from a linked list */
ListNode *getListNode(ListNode *head, int val) {
while (head != NULL && head->val != val) {
head = head->next;
}
return head;
}

/* Free the memory allocated to a linked list */
/* 释放分配给链表的内存空间 */
void freeMemoryLinkedList(ListNode *cur) {
// 释放内存
ListNode *pre;
Expand Down
17 changes: 10 additions & 7 deletions codes/c/utils/print_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
extern "C" {
#endif

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

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

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

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

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

/* Print a binary tree */
/* 打印二叉树 */
void printTree(TreeNode *root) {
printTreeHelper(root, NULL, false);
}

/* Print a Heap */
/* 打印堆 */
void printHeap(int arr[], int size) {
TreeNode *root;
printf("堆的数组表示:");
Expand Down
14 changes: 3 additions & 11 deletions codes/cpp/utils/list_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

using namespace std;

/* Definition for a singly-linked list node */
/* 链表节点 */
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {
}
};

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

/* Get a list node with specific value from a linked list */
ListNode *getListNode(ListNode *head, int val) {
while (head != nullptr && head->val != val) {
head = head->next;
}
return head;
}

/* Free the memory allocated to a linked list */
/* 释放分配给链表的内存空间 */
void freeMemoryLinkedList(ListNode *cur) {
// 释放内存
ListNode *pre;
Expand Down
31 changes: 15 additions & 16 deletions codes/cpp/utils/print_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ string strRepeat(string str, int n) {
return os.str();
}

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

/* Print a vector */
/* 打印列表 */
template <typename T> void printVector(vector<T> list) {
cout << getVectorString(list) << '\n';
}

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

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

/**
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
struct Trunk {
Trunk *prev;
string str;
Expand All @@ -98,7 +94,6 @@ struct Trunk {
}
};

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

/* Print a binary tree */
/**
* 打印二叉树
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
void printTree(TreeNode *root, Trunk *prev, bool isRight) {
if (root == nullptr) {
return;
Expand Down Expand Up @@ -140,12 +139,12 @@ void printTree(TreeNode *root, Trunk *prev, bool isRight) {
printTree(root->left, &trunk, false);
}

/* The interface of the tree printer */
/* 打印二叉树 */
void printTree(TreeNode *root) {
printTree(root, nullptr, false);
}

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

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

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

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

/* Print a Heap (PriorityQueue) */
/* 打印堆(优先队列) */
template <typename T, typename S, typename C> void printHeap(priority_queue<T, S, C> &heap) {
vector<T> vec = Container(heap);
cout << "堆的数组表示:";
Expand Down
12 changes: 2 additions & 10 deletions codes/csharp/utils/ListNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace hello_algo.utils;

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

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

/* Get a list node with specific value from a linked list */
public static ListNode? GetListNode(ListNode? head, int val) {
while (head != null && head.val != val) {
head = head.next;
}
return head;
}

public override string? ToString() {
List<string> list = [];
var head = this;
Expand Down
Loading

0 comments on commit 034ee65

Please sign in to comment.