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
This commit is contained in:
Yudong Jin
2024-03-31 03:06:41 +08:00
committed by GitHub
parent cfe8281aee
commit 034ee65e9a
35 changed files with 133 additions and 271 deletions

View File

@@ -6,7 +6,7 @@
#include "../utils/common.h"
#define ARRAY_SIZE 10
#define SIZE 10
/* 比较两个浮点数的大小 */
int compare_float(const void *a, const void *b) {
@@ -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. 将数组元素分配到各个桶中
@@ -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++;
}
@@ -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];
}
@@ -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;
}

View File

@@ -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() {

View File

@@ -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;
@@ -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;

View File

@@ -18,7 +18,7 @@
extern "C" {
#endif
/* Print an Array */
/* 打印数组 */
void printArray(int arr[], int size) {
if (arr == NULL || size == 0) {
printf("[]");
@@ -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("[]");
@@ -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;
@@ -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;
@@ -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;
@@ -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("堆的数组表示:");