Skip to content

Commit

Permalink
Merge branch 'krahets:master' into rust-computational_complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
xBLACKICEx authored Jan 14, 2023
2 parents a5425b6 + 33e84ff commit 8317ffb
Show file tree
Hide file tree
Showing 135 changed files with 4,948 additions and 315 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
# Editor
.vscode/
.idea/
cmake-build-debug/
hello-algo.iml
*.dSYM/

# mkdocs files
site/
Expand Down
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.9.0-alpine

RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mkdocs-material==9.0.2

WORKDIR /app

COPY codes /app/codes
COPY docs /app/docs
COPY mkdocs.yml /app/mkdocs.yml

RUN mkdir ./docs/overrides && mkdocs build

EXPOSE 8000

CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"]
12 changes: 12 additions & 0 deletions codes/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.10)
project(hello_algo C)

set(CMAKE_C_STANDARD 11)

include_directories(./include)

add_subdirectory(include)
add_subdirectory(chapter_computational_complexity)
add_subdirectory(chapter_array_and_linkedlist)
add_subdirectory(chapter_sorting)
add_subdirectory(chapter_tree)
2 changes: 2 additions & 0 deletions codes/c/chapter_array_and_linkedlist/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(array array.c)
add_executable(linked_list linked_list.c)
88 changes: 88 additions & 0 deletions codes/c/chapter_array_and_linkedlist/linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* File: linked_list.c
* Created Time: 2022-01-12
* Author: Zero ([email protected])
*/

#include "../include/include.h"

/* 在链表的结点 n0 之后插入结点 P */
void insert(ListNode* n0, ListNode* P) {
ListNode *n1 = n0->next;
n0->next = P;
P->next = n1;
}

/* 删除链表的结点 n0 之后的首个结点 */
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
// 详见 https://github.com/krahets/hello-algo/pull/244#discussion_r1067863888
void removeNode(ListNode* n0) {
if (!n0->next)
return;
// n0 -> P -> n1
ListNode *P = n0->next;
ListNode *n1 = P->next;
n0->next = n1;
// 释放内存
free(P);
}

/* 访问链表中索引为 index 的结点 */
ListNode* access(ListNode* head, int index) {
while (head && head->next && index) {
head = head->next;
index--;
}
return head;
}

/* 在链表中查找值为 target 的首个结点 */
int find(ListNode* head, int target) {
int index = 0;
while (head) {
if (head->val == target)
return index;
head = head->next;
index++;
}
return -1;
}


/* Driver Code */
int main() {
/* 初始化链表 */
// 初始化各个结点
ListNode* n0 = newListNode(1);
ListNode* n1 = newListNode(3);
ListNode* n2 = newListNode(2);
ListNode* n3 = newListNode(5);
ListNode* n4 = newListNode(4);
// 构建引用指向
n0->next = n1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
printf("初始化的链表为\r\n");
printLinkedList(n0);

/* 插入结点 */
insert(n0, newListNode(0));
printf("插入结点后的链表为\r\n");
printLinkedList(n0);

/* 删除结点 */
removeNode(n0);
printf("删除结点后的链表为\r\n");
printLinkedList(n0);

/* 访问结点 */
ListNode* node = access(n0, 3);
printf("链表中索引 3 处的结点的值 = %d\r\n", node->val);

/* 查找结点 */
int index = find(n0, 2);
printf("链表中值为 2 的结点的索引 = %d\r\n", index);

return 0;
}
2 changes: 2 additions & 0 deletions codes/c/chapter_computational_complexity/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(time_complexity time_complexity.c )
add_executable(worst_best_time_complexity worst_best_time_complexity.c)
12 changes: 7 additions & 5 deletions codes/c/chapter_computational_complexity/time_complexity.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ int bubbleSort(int *nums, int n) {
for (int i = n - 1; i > 0; i--) {
// 内循环:冒泡操作
for (int j = 0; j < i; j++) {
// 交换 nums[j] 与 nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // 元素交换包含 3 个单元操作
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // 元素交换包含 3 个单元操作
}
}
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ int main(int argc, char *argv[]) {
nums = NULL;
}
}
getchar();
return 0;
}
2 changes: 2 additions & 0 deletions codes/c/chapter_sorting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(bubble_sort bubble_sort.c)
add_executable(insertion_sort insertion_sort.c)
12 changes: 6 additions & 6 deletions codes/c/chapter_sorting/bubble_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../include/include.h"

/* 冒泡排序 */
void bubble_sort(int nums[], int size) {
void bubbleSort(int nums[], int size) {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = 0; i < size - 1; i++)
{
Expand All @@ -25,7 +25,7 @@ void bubble_sort(int nums[], int size) {
}

/* 冒泡排序(标志优化)*/
void bubble_sort_with_flag(int nums[], int size) {
void bubbleSortWithFlag(int nums[], int size) {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = 0; i < size - 1; i++)
{
Expand All @@ -50,15 +50,15 @@ void bubble_sort_with_flag(int nums[], int size) {
/* Driver Code */
int main() {
int nums[6] = {4, 1, 3, 1, 5, 2};
printf("冒泡排序后:\n");
bubble_sort(nums, 6);
printf("冒泡排序后: ");
bubbleSort(nums, 6);
for (int i = 0; i < 6; i++)
{
printf("%d ", nums[i]);
}

printf("优化版冒泡排序后:\n");
bubble_sort_with_flag(nums, 6);
printf("\n优化版冒泡排序后: ");
bubbleSortWithFlag(nums, 6);
for (int i = 0; i < 6; i++)
{
printf("%d ", nums[i]);
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/insertion_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void insertionSort(int nums[], int size) {
int main() {
int nums[] = {4, 1, 3, 1, 5, 2};
insertionSort(nums, 6);
printf("插入排序完成后 nums = \n");
printf("插入排序完成后 nums = ");
for (int i = 0; i < 6; i++)
{
printf("%d ", nums[i]);
Expand Down
4 changes: 4 additions & 0 deletions codes/c/chapter_tree/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_executable(binary_search binary_tree.c)
add_executable(binary_tree_bfs binary_tree_bfs.c)
add_executable(binary_tree_dfs binary_tree_dfs.c)
add_executable(binary_search_tree binary_search_tree.c)
8 changes: 8 additions & 0 deletions codes/c/chapter_tree/binary_search_tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* File: binary_search_tree.c
* Created Time: 2023-01-11
* Author: Reanon ([email protected])
*/

#include "../include/include.h"

42 changes: 42 additions & 0 deletions codes/c/chapter_tree/binary_tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* File: binary_tree.c
* Created Time: 2023-01-11
* Author: Reanon ([email protected])
*/

#include "../include/include.h"

/* Driver Code */
int main() {
/* 初始化二叉树 */
// 初始化结点
TreeNode* n1 = newTreeNode(1);
TreeNode* n2 = newTreeNode(2);
TreeNode* n3 = newTreeNode(3);
TreeNode* n4 = newTreeNode(4);
TreeNode* n5 = newTreeNode(5);
// 构建引用指向(即指针)
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
printf("初始化二叉树\n");
printTree(n1);

/* 插入与删除结点 */
TreeNode* P = newTreeNode(0);
// 在 n1 -> n2 中间插入结点 P
n1->left = P;
P->left = n2;
printf("插入结点 P 后\n");
printTree(n1);

// 删除结点 P
n1->left = n2;
// 释放内存
free(P);
printf("删除结点 P 后\n");
printTree(n1);

return 0;
}
66 changes: 66 additions & 0 deletions codes/c/chapter_tree/binary_tree_bfs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* File: binary_tree_bfs.c
* Created Time: 2023-01-11
* Author: Reanon ([email protected])
*/

#include "../include/include.h"

/* 层序遍历 */
int *levelOrder(TreeNode *root, int *size) {
/* 辅助队列 */
int front, rear;
int index, *arr;
TreeNode *node;
TreeNode **queue;

/* 辅助队列 */
queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
// 队列指针
front = 0, rear = 0;
// 加入根结点
queue[rear++] = root;
// 初始化一个列表,用于保存遍历序列
/* 辅助数组 */
arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE);
// 数组指针
index = 0;
while (front < rear) {
// 队列出队
node = queue[front++];
// 保存结点
arr[index++] = node->val;
if (node->left != NULL) {
// 左子结点入队
queue[rear++] = node->left;
}
if (node->right != NULL) {
// 右子结点入队
queue[rear++] = node->right;
}
}
// 更新数组长度的值
*size = index;
arr = realloc(arr, sizeof(int) * (*size));
return arr;
}


/* Driver Code */
int main() {
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
int nums[] = {1, 2, 3, NIL, 5, 6, NIL};
int size = sizeof(nums) / sizeof(int);
TreeNode *root = arrToTree(nums, size);
printf("初始化二叉树\n");
printTree(root);

/* 层序遍历 */
// 需要传入数组的长度
int *arr = levelOrder(root, &size);
printf("层序遍历的结点打印序列 = ");
printArray(arr, size);

return 0;
}
Loading

0 comments on commit 8317ffb

Please sign in to comment.