Skip to content

Commit 13e188d

Browse files
committed
note update
- dsa
1 parent 61922ef commit 13e188d

File tree

11 files changed

+101
-124
lines changed

11 files changed

+101
-124
lines changed

content/Algorithm/Sorting.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Author Profile:
66
tags:
77
- dsa
88
Creation Date: 2024-01-03, 14:26
9-
Last Date: 2024-03-11T20:07:26+08:00
9+
Last Date: 2024-03-12T12:29:02+08:00
1010
References:
1111
draft:
1212
description: Trying to sort my life out.
@@ -42,14 +42,14 @@ description: Trying to sort my life out.
4242

4343
>[!example]- Sorting algorithms that are unstable
4444
> **Bogo Sort**
45-
> - Random [[Permutation]] may sway elements with the same value
45+
> - Random [[Permutation]] may swap elements with the same value
4646
>
4747
> **[[#Selection Sort]]**
4848
> - Imagine in iteration $j$, the element at position $j$ is $8$ and element at position $j+1$ is $8$ too. Then the smallest element in the range $[j, k]$ is $7$. We swap $7$ with the $8$ at position $j$. As you can see, the order of the 2 same value is changed. Thus, unstable
4949
5050
### Divide-and-Conquer Sorting
5151
- **Divide**: split array into two halves
52-
- **[[Recursion]] up**: sort the two halves
52+
- **[[Recursion]] up**: hit the base case, kickstart sorting the two halves
5353
- **Combine**: merge the sorted halves
5454

5555
## Bubble Sort
@@ -83,7 +83,7 @@ description: Trying to sort my life out.
8383
8484
>[!note]- Time Complexity
8585
> **Best-case**
86-
> - $O(n^2)$, because at each iteration, we can only find the current smallest element. Even if we are given a fully sorted array, we need to perform$n$ iterations, in order to have the confidence to say that the array is sorted
86+
> - $O(n^2)$, because at each iteration, we can only find the current smallest element. Even if we are given a fully sorted array, we need to perform $n$ iterations, in order to have the confidence to say that the array is sorted
8787
>
8888
> **Average-case**
8989
> - $O(n^2)$, assume inputs are chosen at random
@@ -96,7 +96,7 @@ description: Trying to sort my life out.
9696
- The idea here is that at each iteration $j$, we are are going to **insert** the element at the $j$ position to the prefix [[Array]] that is $j-1$ long, such that the new prefix array which is $j$ long remains sorted
9797

9898
>[!important] Loop Invariant
99-
> At the end of iteration $j$, the first $j$ items in the array are **sorted order**.
99+
> At the end of iteration $j$, the first $j$ items in the array are in **sorted order**.
100100
101101
>[!note]- Time Complexity
102102
> **Best-case**
@@ -136,9 +136,11 @@ description: Trying to sort my life out.
136136
> Guess the time complexity and verify it with the reoccurrence we obtained.
137137
138138
>[!caution] Slow on small arrays!
139-
> Merge sort has a space complexity of $O(n)$, we know allocation of different arrays are scattered in the [[Main Memory]]. When we need to work multiple arrays we sacrifice the performance gain from [[CPU Cache#Cache Locality]]. And the [[Recursion]] nature of the algorithm comes with extra overhead. Recursion is also less predicable, thus negative impact on [[Branch Prediction]].
139+
> The allocation of different arrays are scattered in the [[Main Memory]]. Merge sort has a space complexity of $O(n)$ with different temporary arrays at each merge layer. Working on multiple arrays means we sacrifice the performance gain from [[CPU Cache#Cache Locality]].
140140
>
141-
> When the array is small, such hardware level impact outweighs the performance gain from the better time complexity.
141+
> The [[Recursion]] nature of the algorithm comes with extra overhead too. Recursion is also less predicable, thus negative impact on [[Branch Prediction]].
142+
>
143+
> When the array is small, such hardware level negative impact outweighs the performance gains from the better time complexity.
142144
143145
>[!caution] Slow on almost sorted array!
144-
> Merge sort's performance is $O(nlogn)$ when the array is almost sorted, because it needs to perform the full divide-and-conquer process regardless how chaotic the given array is. While [[#Bubble Sort]] and [[#Insertion Sort]] have a time complexity of $O(n)$ only.
146+
> Merge sort's performance is still $O(nlogn)$ when the array is almost sorted, because it needs to perform the full divide-and-conquer process regardless how chaotic the given array is. While [[#Bubble Sort]] and [[#Insertion Sort]] have a time complexity of $O(n)$.

content/C/C Terminologies.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ Author Profile:
66
tags:
77
- c
88
Creation Date: 2023-12-22, 23:53
9-
Last Date: 2023-12-22T23:54:36+08:00
9+
Last Date: 2024-03-12T12:20:39+08:00
1010
References:
11-
draft:
11+
draft:
1212
---
1313
## Abstract
1414
---
1515

16-
| Terminology | Meaning |
17-
| ----------- | ------- |
18-
| C Vector | Basically [[Dynamic Array (List)]] |
19-
| C Structs | Allow grouping of heterogenous data |
16+
| Terminology | Meaning |
17+
| ----------- | ----------------------------------- |
18+
| C Vector | Basically [[Array#Dynamic Array]] |
19+
| C Structs | Allow grouping of heterogenous data |

content/Data Structure/Array.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
Author:
3+
- Xinyang YU
4+
Author Profile:
5+
- https://linkedin.com/in/xinyang-yu
6+
tags:
7+
- dsa
8+
Creation Date: 2023-10-08T20:10:00
9+
Last Date: 2024-03-12T12:20:26+08:00
10+
References:
11+
---
12+
## Abstract
13+
---
14+
- A [[Data Structure#Linear]] collection of elements of the same [[Datatype]] that are stored in [[Data Structure#Continuous Memory]]. The next node is obtained by adding a **constant value** to the [[Memory Address]] of current node
15+
- Can be used to implement [[Hash Map]] when keys are fixed
16+
- [Leetcode questions](https://github.com/youngyangyang04/leetcode-master#%E6%95%B0%E7%BB%84)
17+
18+
>[!caution] Fixed Size
19+
> If we want to expand, we have to create another bigger array & **copy all the elements** to the new array which is very time consuming
20+
21+
>[!attention] Element Removal
22+
> We can't delete elements in arrays, we can only overwrite
23+
24+
### Time Complexity
25+
>[!note]- Search
26+
> $O(1)$ to search for a value.
27+
28+
>[!note]- Indexing
29+
> It is $O(1)$ to index any elements in an array. The indexing formula is `elementAddr = firtstElementAddr + elementLength * elementIndex`, `elementIndex` is $0$ when we try to access the first element
30+
31+
>[!note]- Insert, Delete
32+
> $O(1)$ at the 2 ends of the array
33+
>
34+
> $O(n)$ in the middle of the array
35+
> - For insert, we have to move all the elements next to the new element one step to right
36+
> - For delete, we have move all element next to the deleted element one step to left
37+
38+
>[!info]- Performance comparison with Linked List when going through all elements
39+
> Array is much faster if there is [[CPU Cache]], otherwise it will be slightly slower. Because Array has to calculate the address of the next element, while [[Linked List]] is already calculated.
40+
>
41+
> The reason why array is faster with CPU Cache is because array is stored in a [[Data Structure#Continuous Memory]] manner. Thus array is able to take advantage of [[CPU Cache#Cache Locality]]
42+
43+
### Contiguous Segment
44+
- A continuous range of [[Array]]
45+
46+
47+
## Dynamic Array
48+
---
49+
- Also known as **List**
50+
- Resizable [[Array]], achieved by building an [[Abstraction (抽象)]] above the Array
51+
52+
53+
>[!tip] Convenient
54+
> Developers don't need to re-write battle-tested logic of re-sizeing Array etc, battery-packed with best practices.
55+
56+
>[!attention] More Resource Intense
57+
> We can't fine tune every Array operations because the implementation details are abstracted away. We only have a limited interface to interact with it.
58+
59+
## Circular Array
60+
---
61+
- Connect the start and end of the [[Array]] to form a loop
62+
- With taking remainder from ``frontIndex % arrayCapacity``, we are able to implement circular array on an array. Take a look at [Visual](https://www.hello-algo.com/chapter_stack_and_queue/queue/#2) for better understanding
63+
- Used to implement [[Queue (FIFO)#Implementation|Queue]]
64+
65+
>[!info] Front Index
66+
> A variable to keep track the index for the start of the circular array.
67+
68+
>[!info] Rear Index
69+
> A variable to keep track the index of the slot after the last element of the circular array, Can be obtained with `frontIndex + arraySize`.

content/Data Structure/Array/Array.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

content/Data Structure/Array/Circular Array.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

content/Data Structure/Array/Dynamic Array (List).md

Lines changed: 0 additions & 20 deletions
This file was deleted.

content/Data Structure/Deque.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Author Profile:
66
tags:
77
- dsa
88
Creation Date: 2023-12-11T20:43:00+08:00
9-
Last Date: 2023-12-11T20:43:00+08:00
10-
References:
9+
Last Date: 2024-03-12T12:18:59+08:00
10+
References:
1111
---
1212
## Basics
1313
---
@@ -17,7 +17,7 @@ References:
1717
>- Aka [[Array]]/[[Linked List]] with limitations
1818
1919

20-
>[!caution]
20+
2121

2222

2323
%%>[!example] Use as
@@ -29,7 +29,7 @@ References:
2929
## Implementation
3030
---
3131

32-
%%>[!note] Implementation wit[](Circular%20Array.md)]]
32+
%%>[!note] Implementation wit[](Array.md#Circular%20Array)]]
3333
>
3434
>- [Visual](https://www.hello-algo.com/chapter_stack_and_queue/queue/#2)
3535
>- push() at the rear index

content/Data Structure/Queue (FIFO).md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Author Profile:
66
tags:
77
- dsa
88
Creation Date: 2023-08-30T12:54:00
9-
Last Date: 2024-03-05T12:39:43+08:00
9+
Last Date: 2024-03-12T12:17:39+08:00
1010
References:
1111
---
1212
## Abstract
@@ -25,11 +25,11 @@ References:
2525
## Implementation
2626
---
2727

28-
>[!note] Implementation with [[Circular Array]]
29-
>
30-
>- [Visual](https://www.hello-algo.com/chapter_stack_and_queue/queue/#2)
31-
>- push() at the rear index
32-
>- pop() & peek() at the head index
28+
>[!note] Implementation with Circular Array
29+
> - [[Array#Circular Array]]
30+
> - [Visual](https://www.hello-algo.com/chapter_stack_and_queue/queue/#2)
31+
> - push() at the rear index
32+
> - pop() & peek() at the head index
3333
3434
>[!note] Implementation with [[Single Linked List]]
3535
>- [Visual](https://www.hello-algo.com/chapter_stack_and_queue/queue/#1)

content/Data Structure/Stack.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tags:
77
- dsa
88
- OS
99
Creation Date: 2023-10-07T16:38:28+08:00
10-
Last Date: 2024-03-05T10:46:36+08:00
10+
Last Date: 2024-03-12T12:21:28+08:00
1111
References:
1212
---
1313
## Abstract
@@ -47,7 +47,7 @@ References:
4747
### Implementation with Array
4848
- [Visual](https://www.hello-algo.com/chapter_stack_and_queue/stack/#2)
4949
- push(), pop() & peek() from the back
50-
- Can use [[Dynamic Array (List)]], then the expansion is handled automatically
50+
- Can use [[Array#Dynamic Array]], then the expansion is handled automatically
5151

5252
### Implementation with Linked List
5353
- Implementation with [[Single Linked List]]

content/cp/Competitive Programming Code Templates.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tags:
88
- java
99
- cpp
1010
Creation Date: 2023-12-29, 16:16
11-
Last Date: 2024-03-04T11:47:07+08:00
11+
Last Date: 2024-03-12T12:21:14+08:00
1212
References:
1313
draft:
1414
---
@@ -19,7 +19,7 @@ A collection of code templates to kickstart the journey of solving interesting p
1919
>[!tip]
2020
>1. Make sure all inputs are read in in sequence
2121
>2. Use at least 64[[Computer Data Representation#Bit]] to prevent integer overflow issue, `long` in [[Java]], `long long` in [[CPP]]
22-
>3. [[Dynamic Array (List)]] over [[Array]]
22+
>3. [[Array#Dynamic Array]] over [[Array]]
2323
2424

2525
## Java Code Template

0 commit comments

Comments
 (0)