# JavaScript Algorithms and Data Structures

This repository contains JavaScript-based examples of many popular algorithms and data structures.

Each algorithm and data structure has its own separate README with related explanations and links for further reading (including ones to YouTube videos).

## Data Structures

A data structure is a particular way of organizing and storing data in a computer so that it can be accessed and modified efficiently. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.

Remember that each data has its own trade-offs. And you need to pay attention more to why you're choosing a certain data structure than to how to implement it.

`B` - Beginner, `A` - Advanced

* `B` [Linked List](/content/projects/src/data-structures/linked-list/index.html)
* `B` [Doubly Linked List](/content/projects/src/data-structures/doubly-linked-list/index.html)
* `B` [Queue](/content/projects/src/data-structures/queue/index.html)
* `B` [Stack](/content/projects/src/data-structures/stack/index.html)
* `B` [Hash Table](/content/projects/src/data-structures/hash-table/index.html)
* `B` [Heap](/content/projects/src/data-structures/heap/index.html) - max and min heap versions
* `B` [Priority Queue](/content/projects/src/data-structures/priority-queue/index.html)
* `A` [Trie](/content/projects/src/data-structures/trie/index.html)
* `A` [Tree](/content/projects/src/data-structures/tree/index.html)
* `A` [Binary Search Tree](/content/projects/src/data-structures/tree/binary-search-tree/index.html)
* `A` [AVL Tree](/content/projects/src/data-structures/tree/avl-tree/index.html)
* `A` [Red-Black Tree](/content/projects/src/data-structures/tree/red-black-tree/index.html)
* `A` [Segment Tree](/content/projects/src/data-structures/tree/segment-tree/index.html) - with min/max/sum range queries examples
* `A` [Fenwick Tree](/content/projects/src/data-structures/tree/fenwick-tree/index.html) (Binary Indexed Tree)
* `A` [Graph](/content/projects/src/data-structures/graph/index.html) (both directed and undirected)
* `A` [Disjoint Set](/content/projects/src/data-structures/disjoint-set/index.html) - a union–find data structure or merge–find set
* `A` [Bloom Filter](/content/projects/src/data-structures/bloom-filter/index.html)
* `A` [LRU Cache](/content/projects/src/data-structures/lru-cache/index.html) - Least Recently Used (LRU) cache

## Algorithms

An algorithm is an unambiguous specification of how to solve a class of problems. It is a set of rules that precisely define a sequence of operations.

### Algorithms by Topic

* **Math**
* `B` [Bit Manipulation](/content/projects/src/algorithms/math/bits/index.html) - set/get/update/clear bits, multiplication/division by two, make negative etc.
* `B` [Binary Floating Point](/content/projects/src/algorithms/math/binary-floating-point/index.html) - binary representation of the floating-point numbers.
* `B` [Factorial](/content/projects/src/algorithms/math/factorial/index.html)
* `B` [Fibonacci Number](/content/projects/src/algorithms/math/fibonacci/index.html) - classic and closed-form versions
* `B` [Prime Factors](/content/projects/src/algorithms/math/prime-factors/index.html) - finding prime factors and counting them using Hardy-Ramanujan's theorem
* `B` [Primality Test](/content/projects/src/algorithms/math/primality-test/index.html) (trial division method)
* `B` [Euclidean Algorithm](/content/projects/src/algorithms/math/euclidean-algorithm/index.html) - calculate the Greatest Common Divisor (GCD)
* `B` [Least Common Multiple](/content/projects/src/algorithms/math/least-common-multiple/index.html) (LCM)
* `B` [Sieve of Eratosthenes](/content/projects/src/algorithms/math/sieve-of-eratosthenes/index.html) - finding all prime numbers up to any given limit
* `B` [Is Power of Two](/content/projects/src/algorithms/math/is-power-of-two/index.html) - check if the number is power of two (naive and bitwise algorithms)
* `B` [Pascal's Triangle](/content/projects/src/algorithms/math/pascal-triangle/index.html)
* `B` [Complex Number](/content/projects/src/algorithms/math/complex-number/index.html) - complex numbers and basic operations with them
* `B` [Radian & Degree](/content/projects/src/algorithms/math/radian/index.html) - radians to degree and backwards conversion
* `B` [Fast Powering](/content/projects/src/algorithms/math/fast-powering/index.html)
* `B` [Horner's method](/content/projects/src/algorithms/math/horner-method/index.html) - polynomial evaluation
* `B` [Matrices](/content/projects/src/algorithms/math/matrix/index.html) - matrices and basic matrix operations (multiplication, transposition, etc.)
* `B` [Euclidean Distance](/content/projects/src/algorithms/math/euclidean-distance/index.html) - distance between two points/vectors/matrices
* `A` [Integer Partition](/content/projects/src/algorithms/math/integer-partition/index.html)
* `A` [Square Root](/content/projects/src/algorithms/math/square-root/index.html) - Newton's method
* `A` [Liu Hui π Algorithm](/content/projects/src/algorithms/math/liu-hui/index.html) - approximate π calculations based on N-gons
* `A` [Discrete Fourier Transform](/content/projects/src/algorithms/math/fourier-transform/index.html) - decompose a function of time (a signal) into the frequencies that make it up

### How to use this repository

**Install all dependencies**

```
npm install
```

**Run ESLint**

You may want to run it to check code quality.

```
npm run lint
```

**Run all tests**

```
npm test
```

**Run tests by name**

```
npm test -- 'LinkedList'
```

**Troubleshooting**

If linting or testing is failing, try to delete the `node_modules` folder and re-install npm packages:

```
rm -rf ./node_modules
npm i
```

Also, make sure that you're using the correct Node version (`>=16`). If you're using [nvm](https://github.com/nvm-sh/nvm) for Node version management you may run `nvm use` from the root folder of the project and the correct version will be picked up.

### Useful Information

#### Big O Notation

_Big O notation_ is used to classify algorithms according to how their running time or space requirements grow as the input size grows.

On the chart below, you may find the most common orders of growth of algorithms specified in Big O notation.

| Big O Notation | Type | Computations for 10 elements | Computations for 100 elements | Computations for 1000 elements |
|-----------------|------|-------------------------------|-------------------------------|-------------------------------|
| **O(1)** |
Constant | 1 | 1 | 1 |
| **O(log N)** |
Logarithmic | 3 | 6 | 9 |
| **O(N)** |
Linear | 10 | 100 | 1000 |
| **O(N log N)** |
 n log(n) | 30 | 600 | 9000 |
| **O(N^2)** |
Quadratic | 100 | 10000 | 1000000 |
| **O(2^N)** |
Exponential | 1024 | 1.26e+29 | 1.07e+301 |
| **O(N!)** |
Factorial | 3628800 | 9.3e+157 | 4.02e+2567 |

### Project Author

[@trekhleb](https://trekhleb.dev/)  
A few more [projects](https://trekhleb.dev/projects/) and [articles](https://trekhleb.dev/blog/) about JavaScript and algorithms on [trekhleb.dev](https://trekhleb.dev/)  
**开源协议:** MIT License  
**项目大小:** 14.9k KB
