You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/Data Structure/Array.md
+17-4Lines changed: 17 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Author Profile:
6
6
tags:
7
7
- dsa
8
8
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
10
10
References:
11
11
---
12
12
## Abstract
@@ -29,14 +29,18 @@ References:
29
29
> $O(n)$ to search for a value.
30
30
31
31
>[!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]]
33
35
34
36
>[!note]- Insert, Delete
35
37
> $O(1)$ at the 2 ends of the array
36
38
>
37
39
> $O(n)$ in the middle of the array
38
40
> - For insert, we have to move all the elements next to the new element one step to right
39
41
> - For delete, we have move all element next to the deleted element one step to left
42
+
>
43
+
> ![[array_delete.gif]]
40
44
41
45
>[!info]- Performance comparison with Linked List when going through all elements
42
46
> 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:
51
55
---
52
56
![[dyanmic_array_memory_allocation.png|500]]
53
57
- 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
> Developers don't need to re-write battle-tested logic of re-sizeing Array etc, battery-packed with best practices.
59
67
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
+
60
71
>[!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.
62
73
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.
63
76
## Circular Array
64
77
---
65
78
- Connect the start and end of the [[Array]] to form a loop
Copy file name to clipboardExpand all lines: content/Data Structure/Linked List.md
+23-14Lines changed: 23 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,13 +7,32 @@ tags:
7
7
- dsa
8
8
- java
9
9
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
11
11
References:
12
12
---
13
13
## Abstract
14
14
---
15
+
![[linked_list.png|700]]
15
16
- 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
16
17
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
+
17
36
>[!code]- Linked list node implemented with Java
18
37
> ```java
19
38
>publicclassNode {
@@ -44,24 +63,14 @@ References:
44
63
45
64
>[!tip]-VirtualNode
46
65
> 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 [[MainMemory]].This means we can add more nodes when there is free memory size that can fit a single node.
53
-
54
-
>[!attention]-CacheMiss!!!
55
-
>Since the connection between 2 nodes are via Pointer, the nodes are scattered around the MainMemory. 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 [[GarbageCollector]], we need to manually release deleted node from the [[AddressSpace#HeapSegment]] to prevent [[AddressSpace#Memory leak]].
59
66
### TimeComplexity
60
67
>[!note]-Search
61
68
> $O(n)$ to search for a value.
62
69
63
70
>[!node]-Indexing
64
-
> $O(n)$, because the desired [[MemoryAddress]] 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 [[MemoryAddress]] 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]]
65
74
66
75
>[!note]+ Insert, Delete
67
76
> $O(1)$, we only need the node's previous node and next node, make the corresponding pointer changes.
0 commit comments