Home

Awesome

image

Data Structures and Algorithms in JavaScript

CircleCI NPM version chat

This is the coding implementations of the DSA.js book and the repo for the NPM package.

In this repository, you can find the implementation of algorithms and data structures in JavaScript. This material can be used as a reference manual for developers, or you can refresh specific topics before an interview. Also, you can find ideas to solve problems more efficiently.

<!-- (Check out the Time Complexity Cheatsheet) --> <!-- [![CircleCI](https://circleci.com/gh/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript.svg?style=svg)](https://circleci.com/gh/amejiarosario/dsa.js-data-structures-and-algorithms-in-javascript) [![Code Style Airbnb](https://img.shields.io/badge/code%20style-Airbnb-brightgreen.svg)](https://github.com/airbnb/javascript) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) [![ProductHunt](https://img.shields.io/badge/product%20hunt-vote-orange.svg)](https://www.producthunt.com/posts/dsa-js) https://bundlephobia.com/result?p=dsa.js https://img.shields.io/bundlephobia/min/dsa.js.svg - 16.7kB https://img.shields.io/github/repo-size/amejiarosario/dsa.js.svg - 98.1 MB -->

Interactive Data Structures

<!-- [](https://embed.kumu.io/85f1a4de5fb8430a10a1bf9c5118e015) -->

Table of Contents

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- END doctoc generated TOC please keep comment here to allow auto update -->

Installation

You can clone the repo or install the code from NPM:

npm install dsa.js

and then you can import it into your programs or CLI

const { LinkedList, Queue, Stack } = require('dsa.js');

For a list of all available data structures and algorithms, see index.js.

Features

Algorithms are an essential toolbox for every programmer.

You will need to mind algorithms runtime when you have to sort data, search for a value in a big dataset, transform data, scale your code to many users, to name a few. 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.

Algorithms + Data Structures = Programs.

Most programming languages and libraries indeed provide implementations for basic data structures and algorithms. However, to make use of data structures properly, you have to know the tradeoffs to choose the best tool for the job.

This material is going to teach you to:

What's Inside

All the code and explanations are available on this repo. You can dig through the links and code examples from the (src folder). However, the inline code examples are not expanded (because of Github's asciidoc limitations), but you can follow the path and see the implementation.

Note: If you prefer to consume the information more linearly, then the book format would be more appropriate for you.

The topics are divided into four main categories, as you can see below:

📈 Algorithms Analysis

<!-- - Computer Science nuggets without all the mumbo-jumbo. - Learn how to compare algorithms using Big O notation. - 8 examples to explain with code how to calculate time complexity. --> <blockquote> <details> <summary> Computer Science nuggets without all the mumbo-jumbo. <i>(Click to expand)</i> </summary>

Computer Science nuggets without all the mumbo-jumbo

Learn to calculate run time from code examples

Translating lines of code to an approximate number of operations


</details> <details> <summary> Learn how to compare algorithms using Big O notation. <i>(Click to expand)</i> </summary>

Learn how to compare algorithms using Big O notation.

Comparing algorithms using Big O notation

Let's say you want to find the duplicates on an array. Using Big O notation, we can compare different solutions that solve the same problem but has a massive difference in how long it takes to do it.


</details> <details> <summary> 8 examples to explain with code how to calculate time complexity. <i>(Click to expand)</i> </summary>

8 examples to explain with code how to calculate time complexity

Most common time complexities

image

<!-- - Constant time: _O(1)_ - Logarithmic time: _O(log n)_ - Linear time: _O(n)_ - Linearithmic time: _O(n log n)_ - Quadratic time: _O(n^2^)_ - Cubic time: _O(n^3^)_ - Exponential time: _O(2^n^)_ - Factorial time: _O(n!)_ -->

Time complexity graph

Most common time complexities


</details> </blockquote>

🥞 Linear Data Structures

<!-- - Understand the ins and outs of the most common data structures. - When to use an Array or Linked List. Know the tradeoffs. - Build a Stack and a Queue from scratch. --> <blockquote> <details> <summary> Understand the ins and outs of the most common data structures. <i>(Click to expand)</i> </summary>

Understand the ins and outs of the most common data structures


</details> <details> <summary> When to use an Array or Linked List. Know the tradeoffs. <i>(Click to expand)</i> </summary>

When to use an Array or Linked List. Know the tradeoffs

Use Arrays when…

Use Linked Lists when:


</details> <details> <summary> Build a List, Stack, and a Queue. <i>(Click to expand)</i> </summary>

Build a List, Stack and a Queue from scratch

Build any of these data structures from scratch:


</details> </blockquote>

🌲 Non-Linear Data Structures

<!-- - Understand one of the most versatile data structure of all: Maps --> <!-- - Know the properties of Graphs and Trees. --> <!-- - Implement a binary search tree for fast lookups. --> <blockquote> <details> <summary> Understand one of the most versatile data structure of all: Hash Maps. <i>(Click to expand)</i> </summary>

HashMaps

Learn how to implement different types of Maps such as:

Also, learn the difference between the different Maps implementations:


</details> <details> <summary> Know the properties of Graphs and Trees. <i>(Click to expand)</i> </summary>

Know the properties of Graphs and Trees

Graphs

Know all the graphs properties with many images and illustrations.

graph example with USA airports

Graphs: data nodes that can have a connection or edge to zero or more adjacent nodes. Unlike trees, nodes can have multiple parents, loops. Code | Graph Time Complexity

Trees

Learn all the different kinds of trees and their properties.

tree data structure properties


</details> <details> <summary> Implement a binary search tree for fast lookups. </summary>

Implement a binary search tree for fast lookups

From unbalanced BST to balanced BST

1                           2
  \                       /   \
   2        =>           1     3
    \
     3

</details> </blockquote>

Algorithmic Toolbox

<!-- - Never get stuck solving a problem with seven simple steps. --> <!-- - Master the most popular sorting algorithms (merge sort, quicksort, insertion sort, etc.) --> <!-- - Learn different approaches to solve problems such as divide and conquer, dynamic programming, greedy algorithms, and backtracking. --> <blockquote> <details> <summary> Never get stuck solving a problem with 7 simple steps. <i>(Click to expand)</i> </summary>

Never get stuck solving a problem with 8 simple steps

  1. Understand the problem
  2. Build a simple example (no edge cases yet)
  3. Brainstorm solutions (greedy algorithm, Divide and Conquer, Backtracking, brute force)
  4. Test your answer on the simple example (mentally)
  5. Optimize the solution
  6. Write code. Yes, now you can code.
  7. Test your written code
  8. Analyse the complexity, both space and time, and make sure to optimize further.

Full details here


</details> <details> <summary> Master the most popular sorting algorithms (merge sort, quicksort, etc.) <i>(Click to expand)</i> </summary>

Master the most popular sorting algorithms

We are going to explore three essential sorting algorithms O(n^2), which have low overhead:

and then discuss efficient sorting algorithms O(n log n) such as:


</details> <details> <summary> Learn different approaches to solve problems such as divide and conquer, dynamic programming, greedy algorithms, and backtracking. <i>(Click to expand)</i> </summary>

Learn different approaches to solve algorithmic problems

We are going to discuss the following techniques for solving algorithms problems:


</details> </blockquote>

FAQ

<details> <summary>How would I apply these to my day-to-day work? <i>(Click to expand)</i></summary> <p> As a programmer, we have to solve problems every day. If you want to solve problems well, it's good to know about a broad range of solutions. Often, it's more efficient to learn existing resources than stumble upon the answer yourself. The more tools and practice you have, the better. This book helps you understand the tradeoffs among data structures and reason about algorithms performance. </p> </details> <details> <summary>Why you created this repo/book?</summary> <p> There are not many books about Algorithms in JavaScript. This material fills the gap. Also, it's good practice :) </p> </details> <details> <summary>Is there anyone I can contact if I have questions about something in particular? </summary> <p> Yes, open an issue or ask questions on the [slack channel](https://dsajs-slackin.herokuapp.com). </p> </details>

Book

This project is also available in a book. You will get a nicely formatted PDF with 180+ pages + ePub and Mobi version.

dsa.js book

Support

Reach out to me at one of the following places!

License

License

<!-- ![](https://data.uxeranalytics.com/image1.png?uxa=github-dsajs) -->