Skip to content

Commit

Permalink
chore: clear
Browse files Browse the repository at this point in the history
  • Loading branch information
IsshikiHugh committed Aug 18, 2024
1 parent 0fdacab commit 723096c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .cache/plugin/git-committers/page-authors.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cache_date": "2024-08-13", "page_authors": {"docs/index.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}, {"login": "IsshikiHugh", "name": "IsshikiHugh", "url": "https://github.com/IsshikiHugh", "avatar": "https://avatars.githubusercontent.com/u/48550237?v=4"}]}, "docs/2-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/3-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/4-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/5-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/6-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/7-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/8-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/9-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}}}
{"cache_date": "2024-08-17", "page_authors": {"docs/index.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}, {"login": "IsshikiHugh", "name": "IsshikiHugh", "url": "https://github.com/IsshikiHugh", "avatar": "https://avatars.githubusercontent.com/u/48550237?v=4"}]}, "docs/2-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/3-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/4-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/5-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/6-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/7-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/8-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}, "docs/9-x.md": {"last_commit_date": "2024-08-10", "authors": [{"login": "NoughtQ", "name": "NoughtQ", "url": "https://github.com/NoughtQ", "avatar": "https://avatars.githubusercontent.com/u/88835299?v=4"}]}}}
48 changes: 22 additions & 26 deletions docs/5-x.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
---
counter: True
---

# Chap 5 Hashing

??? abstract "核心知识"
Expand Down Expand Up @@ -147,9 +143,9 @@ $$
};
typedef Position List;

/* List *TheList will be an array of lists, allocated later */
/* The lists use headers (for simplicity), */
/* though this wastes space */
/* List *TheList will be an array of lists, allocated later */
/* The lists use headers (for simplicity), */
/* though this wastes space */
struct HashTbl
{
int TableSize;
Expand All @@ -158,7 +154,7 @@ $$
```

+ 创建空表

``` c
HashTable InitializeTable(int TableSize)
{
Expand All @@ -170,21 +166,21 @@ $$
return NULL;
}
H = (HashTable)malloc(sizeof(struct HashTbl)); // Allocate table
if (H == NULL)
if (H == NULL)
FatalError("Out of Space!!!");
H->TableSize = NextPrime(TableSize); // Better be prime
H->TheLists = malloc(sizeof(List) * H->TableSize); // Array of lists
if (H->TheLists == NULL)
FatalError("Out of space!!!");
for(i = 0; i < H->TableSize; i++)
if (H->TheLists == NULL)
FatalError("Out of space!!!");
for(i = 0; i < H->TableSize; i++)
{ // Allocate list headers
H->TheLists[i] = malloc(sizeof(struct ListNode)); // Slow!
if ( H->TheLists[i] == NULL )
FatalError("Out of space!!!");
else
H->TheLists[i] = malloc(sizeof(struct ListNode)); // Slow!
if ( H->TheLists[i] == NULL )
FatalError("Out of space!!!");
else
H->TheLists[i]->Next = NULL;
}
return H;
}
return H;
}
```

Expand All @@ -197,7 +193,7 @@ $$
注意:散列表的开头,以及每个篮子都是有"空头"的,这主要是为了删除操作的方便。若没有删除操作,则最好不要用空头,这样可以节省空间

+ 从散列表中找键

``` c
Position Find(ElementType Key, HashTable H)
{
Expand All @@ -215,7 +211,7 @@ $$
```

+ 将键插入散列表内(放在篮子的最上(前)面)

``` c
void Insert(ElementType Key, HashTable H)
{
Expand Down Expand Up @@ -277,14 +273,14 @@ Algorithm: insert key into an array of hash table

最简单的冲突解决函数为**线性探测(linear probing)**:f(i) = i,它仅是一个线性函数

!!! example
!!! example

<div style="text-align: center; margin-top: 15px;">
<img src="imgs/C5/Quicker_20240605_213411.png" width="70%" style="margin: 0 auto;">
</div>

对比[前面的例子](#general-idea),可以发现:虽然加载密度得到不小的提升,但是平均搜索时间比较长。

线性探测中预期探测次数关于加载密度的表达式:

$$
Expand Down Expand Up @@ -414,12 +410,12 @@ $$
Position CurrentPos;
int CollisionNum = 0;
CurrentPos = Hash(Key, H->TableSize);
while (H->TheCells[CurrentPos].Info != Empty &&
H->TheCells[CurrentPos].Element != Key)
while (H->TheCells[CurrentPos].Info != Empty &&
H->TheCells[CurrentPos].Element != Key)
{
CurrentPos += 2 * ++CollisionNum - 1; // 1
if (CurrentPos >= H->TableSize) // 2
CurrentPos -= H->TableSize;
CurrentPos -= H->TableSize;
}
return CurrentPos; // 3
}
Expand Down Expand Up @@ -502,7 +498,7 @@ $$
for (i = 0; i < OldSize; i++)
if (OldCells[i].Info == Legitimate)
Insert(OldCells[i].Element, H);

free(OldCells);

return H;
Expand Down
16 changes: 6 additions & 10 deletions docs/6-x.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
---
counter: True
---

# Chap 6 Priority Queues(Heaps)

??? abstract "核心知识"
Expand Down Expand Up @@ -39,7 +35,7 @@ counter: True

+ 虽然理论上它的插入和删除操作时间复杂度为$O(\log n)$,然而实际应用中**插入**操作是**随机**的,这会导致树的不平衡,从而影响时间复杂度
+ 虽然**删除**操作并不是随机的,但我们只要找最小节点并且删除它,这会导致左子树越来越小,破坏了树的平衡,从而影响复杂度
+ 我们可能想到构建一棵平衡的树,比如 AVL 树。但是 AVL 树中的很多操作在优先队列中用不到,且它的指针使用很危险。
+ 我们可能想到构建一棵平衡的树,比如 AVL 树。但是 AVL 树中的很多操作在优先队列中用不到,且它的指针使用很危险。

## ADT Model

Expand Down Expand Up @@ -144,7 +140,7 @@ PriorityQueue Initiailize(int MaxElments)
+ 插入以后要调整节点的位置:
+ 将该节点与其父节点比较,如果比父节点小,则将父节点往下移
+ 重复该步骤,直至比当前比较的节点更大则停止,此时的位置即为新节点的位置
这种技巧被称为“**上滤(percolate up)**”。
???+ play "动画演示"
Expand Down Expand Up @@ -191,7 +187,7 @@ PriorityQueue Initiailize(int MaxElments)
+ 先将它放入根节点的位置(因为”删除“操作,这个位置空出来了),再让它和左右孩子比较
+ 如果比左右孩子都大,则要将最小的孩子放入根节点
+ 接着将它与这个孩子的左右孩子比较,……,直至比左右孩子都小为止
这种技巧被称为“**下滤(percolate down)**”。
???+ play "动画演示"
Expand Down Expand Up @@ -311,7 +307,7 @@ for (i = N / 2; i > 0; i--)
### The Selection Problem

!!! question "问题"

给定$N$个元素,找到第$k$大的元素

!!! note "算法"
Expand All @@ -325,7 +321,7 @@ for (i = N / 2; i > 0; i--)
=== "算法2"

先取前$k$个数并对这些数排好序,再将剩余 $N - k$ 个数分别与这 $k$ 个数比较:如果比这 $k$ 个数中最小的数更大,将最小的数踢出去,让待判断的数进去并放入正确的位置。

时间复杂度为 $O(N \cdot k)$。最坏情况 $k = \lceil N/2 \rceil$ 时,时间复杂度为 $O(N^2)$

---
Expand Down Expand Up @@ -354,7 +350,7 @@ for (i = N / 2; i > 0; i--)
如果我们通过循环每个 tick 来处理整个程序的话,那么运行时间跟顾客和时间的数量无关,而且运行效率很低。因此我们**根据最近时间发生的事件来增加 tick**,这分为两种情况:下一位顾客的到来,有一位顾客离开。

对于正在排队的顾客,我们用**队列**来存储;对于每位顾客离开时间的存储,我们用**优先队列**存储,以便我们找到最近发生的事件。

时间复杂度:$O(C \log(k + 1))$

## d-Heaps
Expand Down
28 changes: 12 additions & 16 deletions docs/7-x.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
---
counter: True
---

# Chap 7 Sorting

??? abstract "核心知识"
Expand Down Expand Up @@ -305,11 +301,11 @@ T(1) &= 1 \notag \\
T(N) &= 2T(\dfrac{N}{2}) + O(N) \notag \\
& = 2^kT(\dfrac{N}{2^k}) + k \cdot O(N) \notag \\
& = N \cdot T(1) + \log N \cdot O(N) \notag \\
& =O(N + N\log N) \notag
& =O(N + N\log N) \notag
\end{align}
$$

>注:另一种证明法见书本$P_{233-234}$
>注:另一种证明法见书本$P_{233-234}$
归并排序需要线性大小的额外内存,且拷贝数组会降低速度,因此在**内部排序**中这种方法不太好用,但是在**外部排序(external sort)**(ads 会讲)中很合适

Expand All @@ -322,13 +318,13 @@ $$
??? code "代码实现"

可以用来打印每一趟归并排序后的结果

```c
void merge_sort( ElementType list[], int N )
{
ElementType extra[MAXN]; /* the extra space required */
int length = 1; /* current length of sublist being merged */
while( length < N ) {
while( length < N ) {
merge_pass( list, extra, N, length ); /* merge list into extra */
output( extra, N );
length *= 2;
Expand Down Expand Up @@ -358,9 +354,9 @@ $$
while (ptr_l < i + length)
sorted[ptr++] = list[ptr_l++];
while (ptr_r < i + 2 * length && ptr_r < N)
sorted[ptr++] = list[ptr_r++];
sorted[ptr++] = list[ptr_r++];
}
}
}

void output( ElementType list[], int N )
{
Expand Down Expand Up @@ -499,8 +495,8 @@ void Quicksort(ElementType A[], int N)
else break; // partition done
}
Swap(&A[i], &A[Right - 1]); // restore pivot
Qsort(A, Left, i - 1); // recursively sort left part
Qsort(A, i + 1, Right); // recursively sort right part
Qsort(A, Left, i - 1); // recursively sort left part
Qsort(A, i + 1, Right); // recursively sort right part
} // end if - the sequence subarray
else
InsertionSort(A + Left, Right - Left + 1);
Expand Down Expand Up @@ -589,7 +585,7 @@ $$T(N) = \dfrac{2}{N}[\sum\limits_{j = 0}^{N - 1}T(j)] + cN \quad \Rightarrow \q
else if (k > i + 1)
Qselect(A, k, i + 1, Right);
}
else
else
InsertionSort(A + Left, Right - Left + 1);
}
```
Expand Down Expand Up @@ -644,7 +640,7 @@ $$T(N) = \dfrac{2}{N}[\sum\limits_{j = 0}^{N - 1}T(j)] + cN \quad \Rightarrow \q
</div>
最坏情况:有 $\lfloor \dfrac{N}{2} \rfloor$ 个环,需要 $\lfloor \dfrac{3N}{2} \rfloor$ 次移动
时间复杂度:$T = O(mN)$,其中 m 为结构体的大小
## General Lower Bound for Sorting
Expand Down Expand Up @@ -742,7 +738,7 @@ s
</div>

=== "法一:最高位排序"

+ 按 $K^0$ 排序:根据花色,创建 4 个篮子

<div style="text-align: center; margin-top: 15px;">
Expand All @@ -754,7 +750,7 @@ s
<div style="text-align: center; margin-top: 15px;">
<img src="imgs/C7/Quicker_20240529_145158.png" width="40%" style="margin: 0 auto;">
</div>

=== "法二:最低位排序"

+ 按 $K^1$ 排序,根据面值,创建13个篮子
Expand Down
18 changes: 7 additions & 11 deletions docs/8-x.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
---
counter: True
---

# Chap 8 The Disjoint Set ADT

??? abstract "核心知识"
Expand Down Expand Up @@ -48,7 +44,7 @@ counter: True

``` c
Algorithm: (Union / Find)
{
{
// step 1: read the relations in
initialize N disjoint sets;
while (read in a~b)
Expand Down Expand Up @@ -132,7 +128,7 @@ SetType Find(ElementType X, DisjSet S);
<div style="text-align: center; margin-top: 15px;">
<img src="imgs/C8/Quicker_20240410_165703.png" width="80%" style="margin: 0 auto;">
</div>

代码实现:

``` c
Expand Down Expand Up @@ -191,7 +187,7 @@ void SetUnion(DisSet S, SetType Rt1, SetType Rt2)
Initialize S[i] = {i} for i = 1,..., 12;
for (k = 1; k <= Size; k++) // 对于每一对i~j
if (Find(i) != Find(j))
SetUnion(Find(i), Find(j));
SetUnion(Find(i), Find(j));
}
```

Expand Down Expand Up @@ -286,7 +282,7 @@ void SetUnion(DisjSet S, SetType Root1, SetType Root2)
else
// 让 X 的父节点为 X 原来父节点的父节点,这样的最终效果是:
// 从根节点到 X 的路径上,除根节点外的所有节点的父节点均为根节点,实现路径压缩
return S[X] = Find(S[X], S);
return S[X] = Find(S[X], S);
}
```

Expand All @@ -298,7 +294,7 @@ void SetUnion(DisjSet S, SetType Root1, SetType Root2)
{
ElementType root, trail, lead; // trail 表示当前处理的节点,lead 表示下一个要处理的节点
for (root = X; S[root] > 0; root = S[root]); // find the root
for (trail = X; trail != root; trail = lead)
for (trail = X; trail != root; trail = lead)
// 将路径上的所有节点的父节点都设为根节点
{
lead = S[trail];
Expand All @@ -321,10 +317,10 @@ k_1M \alpha(M, N) \le T(M, N) \le k_2M \alpha(M, N)
$$
即并查集最坏情况的时间复杂度为:$\Theta(M\alpha (M, N))$

**阿克曼函数(Ackermann's Function)**:$\alpha (M, N)$
**阿克曼函数(Ackermann's Function)**:$\alpha (M, N)$

$$
A(i, j) =
A(i, j) =
\begin{cases}
2^j & i = 1 \text{ and } j \ge 1 \\
A(i - 1, 2) & i \ge 2 \text{ and } j = 1 \\
Expand Down
Loading

0 comments on commit 723096c

Please sign in to comment.