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
> This is the coding implementations of the [DSA.js book](https://books.adrianmejia.com/dsajs-data-structures-algorithms-javascript/).
7
+
> This is the coding implementations of the [DSA.js book](https://books.adrianmejia.com/dsajs-data-structures-algorithms-javascript/) and the repo for the [npm package](https://www.npmjs.com/package/dsa.js).
8
8
9
-
> In this repository you can find classical algorithms and data structures implemented and explained in JavaScript. it can be used as a reference manual where developers can refresh specific topics before an interview or looking for ideas to solve a problem optimally.
9
+
> In this repository, you can find classical algorithms and data structures implemented and explained in JavaScript. It can be used as a reference manual where developers can refresh specific topics before an interview or looking for ideas to solve a problem more optimally.
10
10
11
11
<!-- (Check out the Time Complexity Cheatsheet) -->
[](https://embed.kumu.io/85f1a4de5fb8430a10a1bf9c5118e015)
25
25
26
26
27
-
## Table of Contents (TODO)
27
+
## Table of Contents
28
28
29
-
<!-- START doctoc -->
30
-
<!-- END doctoc -->
29
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
30
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
31
+
32
+
33
+
-[Installation](#installation)
34
+
-[Features](#features)
35
+
-[What's Inside](#whats-inside)
36
+
-[📈 Algorithms Analysis](#-algorithms-analysis)
37
+
-[🥞 Linear Data Structures](#-linear-data-structures)
38
+
-[🌲 Non-Linear Data Structures](#-non-linear-data-structures)
-[Linear Data Structures](#linear-data-structures)
47
+
-[Non-Linear Data Structures](#non-linear-data-structures)
48
+
-[Algorithms](#algorithms)
49
+
50
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
31
51
32
52
33
53
## Installation
@@ -49,15 +69,17 @@ For a full list of all the exposed data structures and algorithms [see](https://
49
69
50
70
## Features
51
71
72
+
Algorithms are an essential toolbox for every programmer.
73
+
74
+
You usually need algorithms when you have to sort data, search for a value, transform data, scale your code to many users just to name a few.
75
+
Algorithms are just the step you follow to solve a problem while data structures are where you store the data for later manipulation. Both combined create programs.
76
+
52
77
> Algorithms + Data Structures = Programs.
53
78
54
-
Algorithms are an essential toolbox for every programmer.
55
-
You usually need them when you have to sort data, search for a value, transform data, scale your code to many users and so on.
56
-
Algorithms are the step you follow to solve a problem while data structures are where you store the data.
57
-
Both combined create programs.
58
79
It's true that most programming languages and libraries provides implementations for basic data structures and algorithms.
59
80
However, to make use of data structures properly, you have to know the tradeoffs so you can choose the best tool for the job.
60
-
That's what you are going to learn here:
81
+
82
+
This material is going to teach you to:
61
83
62
84
- 🛠 Apply strategies to tackle algorithm questions. Never to get stuck again. Ace those interviews!
63
85
- ✂️ Construct efficient algorithms. Learn how to break down problems in manageable pieces.
@@ -66,187 +88,340 @@ That's what you are going to learn here:
66
88
67
89
## What's Inside
68
90
69
-
### 📈 Algorithms Analysis
91
+
All the code and explanations are available on this repo. You can dig through the links and code examples from the ([src folder](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/tree/master/src)). However, the inline code examples are not expanded (because of Github's asciidoc limitations) but you can follow the path and see the implementation.
92
+
93
+
_Note: If you prefer to consume the information in a more linear fashion then the [book format](https://books.adrianmejia.com/dsajs-data-structures-algorithms-javascript/) would be more appropriate for you._
94
+
95
+
The topics are divided in 4 main categories as you can see below:
70
96
71
-
- Computer Science nuggets without all the mumbo-jumbo.
<!-- - Computer Science nuggets without all the mumbo-jumbo.
72
102
- Learn how to compare algorithms using Big O notation.
73
-
- 8 examples to explain with code how to calculate time complexity.
103
+
- 8 examples to explain with code how to calculate time complexity.-->
74
104
75
-
### 🥞 Linear Data Structures
105
+
<blockquote>
76
106
77
-
- Understand the ins and outs of the most common data structures.
78
-
- When to use an Array or Linked List. Know the tradeoffs.
79
-
- Build a Stack and a Queue from scratch.
107
+
<details>
108
+
<summary>
109
+
Computer Science nuggets without all the mumbo-jumbo
110
+
</summary>
80
111
81
-
### 🌲 Non-Linear Data Structures
82
-
- Understand how hash maps and sets work.
83
-
- Know the properties of Graphs and Trees.
84
-
- Implement a binary search tree for fast lookups.
112
+
---
85
113
86
-
### ⚒ Algorithms Techniques
114
+
### [Computer Science nuggets without all the mumbo-jumbo](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/algorithms-analysis.adoc)
87
115
88
-
- Never get stuck solving a problem with 7 simple steps.
89
-
- Master the most popular sorting algorithms (mergesort, quicksort, insertion sort, ...)
90
-
- Learn different approaches to solve problems such as divide and conquer, dynamic programming, greedy algorithms, and backtracking.
116
+
#### Learn to calculate run time from code examples
91
117
92
-
## FAQ
118
+

93
119
94
-
<details>
95
-
<summary>How would I apply these to my day-to-day work?</summary>
96
-
<p>
97
-
As a programmer, we have to solve problems every day. If you want to solve problems well, then it's good to
98
-
know about a broad range of solutions. A lot of times, it's more efficient to learn existing resources than
99
-
stumble upon the answer yourself. The more tools and practice you have, the better. This book helps you
100
-
understand the tradeoffs among data structures and reason about algorithms performance.
101
-
</p>
102
-
</details>
120
+
---
103
121
104
-
<details>
105
-
<summary>Hey OP, why you created this repo/book?</summary>
106
-
<p>
107
-
There are not many books about Algorithms in JavaScript. This material fills the gap.
108
-
Also, it's good practice :)
109
-
</p>
110
122
</details>
111
123
112
124
<details>
113
-
<summary>Is there anyone I can contact if I have questions about something in particular?
114
-
</summary>
115
-
<p>
116
-
Yes, open an issue or ask questions on the slack channel.
117
-
</p>
118
-
</details>
125
+
<summary>
126
+
Learn how to compare algorithms using Big O notation.
127
+
</summary>
119
128
120
-
## Support
129
+
---
121
130
122
-
Reach out to me at one of the following places!
131
+
### [Learn how to compare algorithms using Big O notation.](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/big-o-examples.adoc#finding-duplicates-in-an-array-na%C3%AFve-approach)
123
132
124
-
- Twitter at <ahref="http://twitter.com/amejiarosario"target="_blank">`@amejiarosario`</a>
125
-
- Slack at <ahref="https://dsajs-slackin.herokuapp.com"target="_blank">`dsajs.slack.com`</a>
133
+
#### Comparing algorithms using Big O notation
126
134
135
+
Let's say you want to find the duplicates on an array.
136
+
Using Big O notation we can compare different implementations that do exactly the same but
137
+
they take different time to complete.
127
138
128
-
## Donations
139
+
-[Optimal solution using a map](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/big-o-examples.adoc#linear-example)
140
+
-[Finding duplicates in an array (naïve approach)](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/big-o-examples.adoc#quadratic-example)
129
141
130
-
The best way to support this project is buying the [book](https://books.adrianmejia.com/dsajs-data-structures-algorithms-javascript/), so I can invest more time into this project and keep improving it.
8 examples to explain with code how to calculate time complexity
149
+
</summary>
135
150
136
151
---
137
-
---
138
-
---
139
152
153
+
[8 examples to explain with code how to calculate time complexity](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/big-o-examples.adoc#summary)
140
154
141
-
##Book
155
+
#### Most common time complexities
142
156
143
-
You can also buy this repo in [book format](https://books.adrianmejia.com/dsajs-data-structures-algorithms-javascript/) that goes deeper into each topic and provide additional illustrations and explanations.
WARNING: Although you can find the [book text on this repo](https://github.com/amejiarosario/dsa.js/blob/master/book/book.adoc), there are some things that doesn't work well with Github's Asciidoc unfortunately. For instance rendering Graphviz diagrams, internal link references, embedding code into docs and so on. For better reading experience and nicer presentation you can get the [PDF format here](https://books.adrianmejia.com/dsajs-data-structures-algorithms-javascript/).
154
-
-->
170
+

155
171
156
-
## Data Structures
172
+
---
157
173
158
-
We are covering the following data structures.
174
+
</details>
175
+
</blockquote>
159
176
160
-
[](https://embed.kumu.io/85f1a4de5fb8430a10a1bf9c5118e015)
177
+
### 🥞 [Linear Data Structures](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/part2.adoc)
178
+
179
+
<!-- - Understand the ins and outs of the most common data structures.
180
+
- When to use an Array or Linked List. Know the tradeoffs.
181
+
- Build a Stack and a Queue from scratch. -->
182
+
183
+
<blockquote>
184
+
185
+
<details>
186
+
<summary>
187
+
Understand the ins and outs of the most common data structures.
188
+
</summary>
161
189
162
-
### Linear Data Structures
190
+
---
191
+
192
+
#### [Understand the ins and outs of the most common data structures](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/part2.adoc)
163
193
164
-
1.**Arrays**: Built-in in most languages so not implemented here. [Array Time complexity](https://github.com/amejiarosario/dsa.js/blob/master/book/chapters/array.adoc#array-complexity)
194
+
195
+
-[Arrays](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/array.adoc): Built-in in most languages so not implemented here. [Array Time complexity](https://github.com/amejiarosario/dsa.js/blob/master/book/chapters/array.adoc#array-complexity)
2.**Linked Lists**: each data node has a link to the next (and
198
+
-[Linked List](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/linked-list.adoc): each data node has a link to the next (and
[Linked List Time Complexity](https://github.com/amejiarosario/dsa.js/blob/master/book/chapters/linked-list.adoc#linked-list-complexity-vs-array-complexity)
3.**Queue**: data flows in a "first-in, first-out" (FIFO) manner.
205
+
-[Queue](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/queue.adoc): data flows in a "first-in, first-out" (FIFO) manner.
4.**Stacks**: data flows in a "last-in, first-out" (LIFO) manner.
211
+
-[Stack](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/stack.adoc): data flows in a "last-in, first-out" (LIFO) manner.
When to use an Array or Linked List. Know the tradeoffs.
223
+
</summary>
224
+
225
+
---
226
+
227
+
#### [When to use an Array or Linked List. Know the tradeoffs](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/linear-data-structures-outro.adoc)
228
+
229
+
Use Arrays when…
230
+
- You need to access data in random order fast (using an index).
231
+
- Your data is multi-dimensional (e.g., matrix, tensor).
232
+
233
+
Use Linked Lists when:
234
+
- You will access your data sequentially.
235
+
- You want to save memory and only allocate memory as you need it.
236
+
- You want constant time to remove/add from extremes of the list.
237
+
238
+
---
239
+
240
+
</details>
241
+
<details>
242
+
<summary>
243
+
Build a List, Stack and a Queue.
244
+
</summary>
245
+
246
+
---
247
+
248
+
#### [Build a List, Stack and a Queue from scratch](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/part2.adoc)
### 🌲 [Non-Linear Data Structures](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/part3.adoc)
261
+
<!-- - Understand one of the most versatile data structure of all: Maps -->
262
+
<!-- - Know the properties of Graphs and Trees. -->
263
+
<!-- - Implement a binary search tree for fast lookups. -->
264
+
265
+
<blockquote>
266
+
<details>
267
+
<summary>
268
+
Understand one of the most versatile data structure of all: Maps
Also, [learn the difference between the different Maps implementations](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/map-hashmap-vs-treemap.adoc):
280
+
281
+
-`HashMap` is more time-efficient. A `TreeMap` is more space-efficient.
282
+
-`TreeMap` search complexity is *O(log n)*, while an optimized `HashMap` is *O(1)* on average.
283
+
-`HashMap`’s keys are in insertion order (or random depending in the implementation). `TreeMap`’s keys are always sorted.
284
+
-`TreeMap` offers some statistical data for free such as: get minimum, get maximum, median, find ranges of keys. `HashMap` doesn’t.
285
+
-`TreeMap` has a guarantee always an *O(log n)*, while `HashMap`s has an amortized time of *O(1)* but in the rare case of a rehash, it would take an *O(n)*.
286
+
287
+
---
288
+
289
+
</details>
290
+
291
+
<details>
292
+
<summary>
293
+
Know the properties of Graphs and Trees.
294
+
</summary>
295
+
296
+
---
297
+
298
+
#### [Know the properties of Graphs and Trees](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/part3.adoc)
1.**Trees**: data nodes has zero or more adjacent nodes a.k.a.
302
+
Know all the graphs properties with many images and illustrations.
303
+
304
+

305
+
306
+
**Graphs**: data **nodes** that can have a connection or **edge** to
307
+
zero or more adjacent nodes. Unlike trees, nodes can have multiple
Learn all the different kinds of trees and its properties.
316
+
317
+

318
+
319
+
-**Trees**: data nodes has zero or more adjacent nodes a.k.a.
189
320
children. Each node can only have one parent node otherwise is a
[TreeMap time complexity](https://github.com/amejiarosario/dsa.js/blob/master/book/chapters/map-hashmap-vs-treemap.adoc#treemap-time-complexity-vs-hashmap)
357
+
<details>
358
+
<summary>
359
+
Implement a binary search tree for fast lookups.
360
+
</summary>
237
361
238
-
3.**Graphs**: data **nodes** that can have a connection or **edge** to
239
-
zero or more adjacent nodes. Unlike trees, nodes can have multiple
#### [Implement a binary search tree for fast lookups](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/tree-binary-search-tree.adoc)
365
+
366
+
- Learn how to add/remove/update values in a tree:
367
+

368
+
369
+
-[How to make a tree balanced?](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/tree-self-balancing-rotations.adoc)
- Never get stuck solving a problem with 7 simple steps.
389
+
- Master the most popular sorting algorithms (mergesort, quicksort, insertion sort, ...)
390
+
- Learn different approaches to solve problems such as divide and conquer, dynamic programming, greedy algorithms, and backtracking.
391
+
392
+
<blockquote>
393
+
<details>
394
+
<summary>
395
+
Never get stuck solving a problem with 7 simple steps
396
+
</summary>
245
397
246
-
## Algorithms
398
+
---
399
+
400
+
#### [Never get stuck solving a problem with 7 simple steps](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/algorithmic-toolbox.adoc)
1. Test your solution on the simple example (mentally)
406
+
1. Optimize the solution
407
+
1. Write Code, yes, now you can code.
408
+
1. Test your written code
409
+
410
+
Full details [here](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/algorithmic-toolbox.adoc)
247
411
248
-
- Sorting algorithms
412
+
---
413
+
414
+
</details>
415
+
<details>
416
+
<summary>
417
+
Master the most popular sorting algorithms (mergesort, quicksort, insertion sort, ...)
418
+
</summary>
249
419
420
+
---
421
+
422
+
#### [Master the most popular sorting algorithms](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/sorting-intro.adoc)
423
+
424
+
We are going to explore three basic sorting algorithms O(n2) which have low overhead:
#### [Learn different approaches to solve algorithmic problems](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/algorithms-intro.adoc)
We are going to discuss the following techniques for solving algorithms problems:
464
+
-[Greedy Algorithms](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/greedy-algorithms.adoc): makes greedy choices using heuristics to find the best solution without looking back.
465
+
-[Dynamic Programming](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/dynamic-programming.adoc): a technique for speeding up recursive algorithms when there are many _overlapping subproblems_. It uses _memoization_ to avoid duplicating work.
466
+
-[Divide and Conquer](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/divide-and-conquer.adoc): _divide_ problems into smaller pieces, _conquer_ each subproblem and then _join_ the results.
467
+
-[Backtracking](https://github.com/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript/blob/master/book/chapters/backtracking.adoc): search _all (or some)_ possible paths. However, it stops and _go back_ as soon as notice the current solution is not working.
468
+
-_Brute Force_: generate all possible solutions and tries all of them. (Use it as a last resort or as the starting point to optimize it with other techniques).
<summary>How would I apply these to my day-to-day work?</summary>
479
+
<p>
480
+
As a programmer, we have to solve problems every day. If you want to solve problems well, then it's good to
481
+
know about a broad range of solutions. A lot of times, it's more efficient to learn existing resources than
482
+
stumble upon the answer yourself. The more tools and practice you have, the better. This book helps you
483
+
understand the tradeoffs among data structures and reason about algorithms performance.
484
+
</p>
485
+
</details>
486
+
487
+
<details>
488
+
<summary>Hey OP, why you created this repo/book?</summary>
489
+
<p>
490
+
There are not many books about Algorithms in JavaScript. This material fills the gap.
491
+
Also, it's good practice :)
492
+
</p>
493
+
</details>
494
+
495
+
<details>
496
+
<summary>Is there anyone I can contact if I have questions about something in particular?
497
+
</summary>
498
+
<p>
499
+
Yes, open an issue or ask questions on the slack channel.
500
+
</p>
501
+
</details>
502
+
503
+
## Support
504
+
505
+
Reach out to me at one of the following places!
506
+
507
+
- Twitter at <ahref="http://twitter.com/amejiarosario"target="_blank">`@amejiarosario`</a>
508
+
- Slack at <ahref="https://dsajs-slackin.herokuapp.com"target="_blank">`dsajs.slack.com`</a>
509
+
510
+
511
+
## Donations
512
+
513
+
The best way to support this project is buying the [book](https://books.adrianmejia.com/dsajs-data-structures-algorithms-javascript/), so I can invest more time into this project and keep improving it.
0 commit comments