Skip to content

Commit fe9da24

Browse files
committed
note update
- dsa
1 parent ab63e38 commit fe9da24

File tree

9 files changed

+40
-18
lines changed

9 files changed

+40
-18
lines changed

content/Data Structure/Array.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Author Profile:
66
tags:
77
- dsa
88
Creation Date: 2023-10-08T20:10:00
9-
Last Date: 2024-03-17T15:13:20+08:00
9+
Last Date: 2024-03-18T18:47:10+08:00
1010
References:
1111
---
1212
## Abstract
@@ -29,14 +29,18 @@ References:
2929
> $O(n)$ to search for a value.
3030
3131
>[!note]- Indexing
32-
> 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
32+
> 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.
33+
>
34+
> ![[array_indexing.png]]
3335
3436
>[!note]- Insert, Delete
3537
> $O(1)$ at the 2 ends of the array
3638
>
3739
> $O(n)$ in the middle of the array
3840
> - For insert, we have to move all the elements next to the new element one step to right
3941
> - For delete, we have move all element next to the deleted element one step to left
42+
>
43+
> ![[array_delete.gif]]
4044
4145
>[!info]- Performance comparison with Linked List when going through all elements
4246
> Array is much faster if there is [[CPU Cache]], otherwise it may be slightly slower. Because Array has to calculate the address of the next element, while [[Linked List]] is already calculated.
@@ -51,15 +55,24 @@ References:
5155
---
5256
![[dyanmic_array_memory_allocation.png|500]]
5357
- Also known as **List**
54-
- Resizable [[Array]], achieved by building an [[Abstraction (抽象)]] above the Array
58+
- A [[Datatype]] that contains a [[Pointer]] to the underlying [[Array]] and other metadata like the capacity of the array and the current size of the array. As shown above, the purple blocks contain the metadata of the dynamic array. The yellow blocks are the actually array that hold the elements
59+
60+
61+
>[!bigbrain] Dynamic array mechanism visualisation
62+
> ![[dyanamic_array_visual.gif]]
5563
5664

5765
>[!success] Convenient
5866
> Developers don't need to re-write battle-tested logic of re-sizeing Array etc, battery-packed with best practices.
5967
68+
>[!success] Secure
69+
> With the built-in expansion mechanism and `length` metadata, we are sure new elements aren't added into [[Memory Address]] that belong to other parts of the [[Process (进程)]]. Thus, ensuring [[Memory Safety]].
70+
6071
>[!attention] More Resource Intense
61-
> 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.
72+
> 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. And Dynamic array comes with metadata to support the different functionalities it offers.
6273
74+
>[!tip] Minimise re-sizing
75+
> If you know how big an array you want, it is usually recommended to set it as the capacity of your dynamic array. This reduce the need of frequent **re-sizing operations** which mean fewer allocation on [[Address Space#Heap Segment]]. Thus, better performance.
6376
## Circular Array
6477
---
6578
- Connect the start and end of the [[Array]] to form a loop

content/Data Structure/Linked List.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,32 @@ tags:
77
- dsa
88
- java
99
Creation Date: 2023-08-05T14:45:43+08:00
10-
Last Date: 2024-03-17T15:04:51+08:00
10+
Last Date: 2024-03-18T18:52:31+08:00
1111
References:
1212
---
1313
## Abstract
1414
---
15+
![[linked_list.png|700]]
1516
- A [[Data Structure#Linear]] collection of elements of the same [[Datatype]] that stored in [[Data Structure#Discrete Memory]]. Each node contains a [[Pointer]] to the next node
1617

18+
19+
>[!success] Adjustable size with minimal impact
20+
> If we want to add in a new node, we just need to modify the pointer of the previous node & the pointer of the next node - in **constant time**. Since the connection between the 2 nodes are via **Pointer**, we can have nodes all around the [[Main Memory]]. This means we can add more nodes as long as there is free memory size that can fit a single node.
21+
>
22+
> ![[linked_list_memory_operation.gif]]
23+
24+
25+
26+
>[!attention] Cache Miss!!!
27+
> Since the connection between 2 nodes are via Pointer, the nodes are scattered around the Main Memory. This means we can't make use of [[CPU Cache#Cache Locality]] and this results in [[CPU Cache#Cache Miss]].
28+
>
29+
> ![[linked_list_cache_miss.gif]]
30+
31+
>[!caution] Memory Leak
32+
> For languages like [[C]] which doesn't come with a [[Garbage Collector]], we need to manually release deleted node from the [[Address Space#Heap Segment]] to prevent [[Address Space#Memory leak]].
33+
34+
35+
1736
>[!code]- Linked list node implemented with Java
1837
> ```java
1938
> public class Node {
@@ -44,24 +63,14 @@ References:
4463
4564
>[!tip]- Virtual Node
4665
> O(1) to access either the **head node** or the **tail node**, this makes handling edge cases & reverting linked list easier.
47-
48-
>[!success]- Adjustable size with great efficiency
49-
> If we want to expand, we just need to modify the pointers of the previous node & the pointers of the next node. **Consistent performance**.
50-
51-
>[!success]- Can be stored in memory in a flexible way
52-
> Since the connection between 2 nodes are via [[Pointer]], we can have nodes all around the [[Main Memory]]. This means we can add more nodes when there is free memory size that can fit a single node.
53-
54-
>[!attention]- Cache Miss!!!
55-
> Since the connection between 2 nodes are via Pointer, the nodes are scattered around the Main Memory. This means we can't make use of [[CPU Cache#Cache Locality]] and resulting in [[CPU Cache#Cache Miss]].
56-
57-
>[!caution]- Memory Leak
58-
> For languages like [[C]] which doesn't come with a [[Garbage Collector]], we need to manually release deleted node from the [[Address Space#Heap Segment]] to prevent [[Address Space#Memory leak]].
5966
### Time Complexity
6067
>[!note]- Search
6168
> $O(n)$ to search for a value.
6269
6370
>[!node]- Indexing
64-
> $O(n)$, because the desired [[Memory Address]] can't be obtained by simply incrementing the index like what we can do with [[Array]]. what we have at the start of every indexing is the memory address of the next node, we need to traverse n-1 nodes in order to access nth node.
71+
> It takes $O(n)$ to obtain the node at a particular position, because the desired [[Memory Address]] can't be obtained by simply incrementing the index like what we can do with [[Array]]. what we have at the start of every indexing is the memory address of the next node, we need to traverse n-1 nodes in order to access nth node.
72+
>
73+
> ![[linked_list_indexing.png]]
6574
6675
>[!note]+ Insert, Delete
6776
> $O(1)$, we only need the node's previous node and next node, make the corresponding pointer changes.
1.88 MB
Loading
1010 KB
Loading
6.85 MB
Loading
1.34 MB
Loading
1.55 MB
Loading
2.34 MB
Loading
2.33 MB
Loading

0 commit comments

Comments
 (0)