This repository contains some popular Data Structures and Algorithms done in JavaScript ES6.
According to Wikipedia:
In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.
The following terms are the foundation terms of a data structure.
- Interface - This represents the set of operation supported by a data structure
- Implementation - This provides the definition of algorithms used in the operations of the data structure i.e. it provides the internal representation of the data structure
- Correctness - a data structure should implement its interface correctly
- Time Complexity - execution time of operations of a data structure should be as small as possible
- Space Complexity - memory usage of a data structure operation should be as little as possible
Data Structure | Access | Search | Insertion | Deletion |
---|---|---|---|---|
Array | 1 | n | n | n |
Stack | n | n | 1 | 1 |
Queue | n | n | 1 | 1 |
Linked List | n | n | 1 | 1 |
Hash Table | - | n | n | n |
Binary Search Tree | n | n | n | n |
B-Tree | log(n) | log(n) | log(n) | log(n) |
AVL Tree | log(n) | log(n) | log(n) | log(n) |
Shown below are the data structures covered here:
- Array
- Linked List
- Doubly Linked List
- Stack
- Queue
- Priority Queue
- Set
- Disjoint Set
- Bloom Filter
- Skip List
- Hash Table
- Graph
- Tree
- Binary Search Tree
- AVL Tree
- Red Black Tree
- Splay Tree
- B-Tree
- Trie
- Min Heap
- Max Heap
- Binomial Heap
- Fibonacci Heap
This is a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.
The following are the algorithms covered here:
- Searching
- Sorting
- Sets
- Longest Increasing Subsequence
- Longest Common Subsequence (LCS)
- Shortest Common Supersequence (SCS)
- Permutations (with and without repetitions)
- Combinations (with and without repetitions)
- Combination Sum - find all combinations that form specific sum
- Maximum Subarray - "Brute Force" and "Dynamic Programming" (Kadane's) versions
- Math
- Factorial
- Fibonacci
- Integer Partition
- Pascal's Triangle
- Least Common Multiple (LCM)
- Primality Test (trial division method)
- Euclidean Algorithm - calculate the Greatest Common Divisor (GCD)
- Sieve of Eratosthenes - finding all prime numbers up to any given limit
- Is Power of Two - check if the number is power of two (naive and bitwise algorithms)
- Trees
- Graphs
- Depth-First Search (DFS)
- Breadth-First Search (BFS)
- Dijkstra Algorithm - finding shortest paths to all graph vertices from single vertex
- Prim’s Algorithm - finding Minimum Spanning Tree (MST) for weighted undirected graph
- Kruskal’s Algorithm - finding Minimum Spanning Tree (MST) for weighted undirected graph
- Travelling Salesman Problem - shortest possible route that visits each city and returns to the origin city
- Others
An algorithmic paradigm is a generic method or approach which underlies the design of a class of algorithms. It is an abstraction higher than the notion of an algorithm, just as an algorithm is an abstraction higher than a computer program.
- Brute Force - look at all the possibilities and selects the best solution
- Linear Search
- Maximum Subarray
- Travelling Salesman Problem - shortest possible route that visits each city and returns to the origin city
- Greedy - choose the best option at the current time, without any consideration for the future
- Prim’s Algorithm - finding MST for weighted undirected graph
- Dijkstra Algorithm - finding shortest path to all graph vertices
- Kruskal’s Algorithm - finding Minimum Spanning Tree (MST) for weighted undirected graph
- Divide and Conquer - divide the problem into smaller parts and then solve those parts
- Quick Sort
- Merge Sort
- Binary Search
- Tower of Hanoi
- Pascal's Triangle
- Euclidean Algorithm - GCD
- Tree Depth-First Search (DFS)
- Graph Depth-First Search (DFS)
- Permutations (with and without repetitions)
- Combinations (with and without repetitions)
- Dynamic Programming - build up a solution using previously found sub-solutions
- Backtracking - similarly to brute force, try to generate all possible solutions, but each time you generate next solution you test if it satisfies all conditions, and only then continue generating subsequent solutions. Otherwise, backtrack, and go on a different path of finding a solution. Normally the DFS traversal of state-space is being used.
- Combination Sum - find all combinations that form specific sum
Name | Best | Average | Worst | Memory | Stable |
---|---|---|---|---|---|
Bubble sort | n | n2 | n2 | 1 | Yes |
Insertion sort | n | n2 | n2 | 1 | Yes |
Selection sort | n2 | n2 | n2 | 1 | No |
Heap sort | n log(n) | n log(n) | n log(n) | 1 | No |
Merge sort | n log(n) | n log(n) | n log(n) | n | Yes |
Quick sort | n log(n) | n log(n) | n2 | log(n) | No |
Shell sort | n log(n) | depends on gap sequence | n (log(n))2 | 1 | No |
Counting sort | n + r | n + r | n + r | n + r | Yes |
Radix sort | n * k | n * k | n * k | n + k | Yes |
This is a theoretical measure of the execution of an algorithm, usually the time or memory needed, given the problem size n, which is usually the number of items. Informally, saying some equation f(n) = O(g(n))
means it is less than some constant multiple of g(n)
.
It is used in Computer Science to describe the performance or complexity of an algorithm. Big-O specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm.
The most optimum algorithm scales in constant time and space. This means it does not care at all about the growth of its inputs. Next best is logarithmic time or space, then linear, linearithmic, quadratic, and exponential. The worst is factorial time or space. In Big-O notation:
- Constant: O(1)
- Logarithmic: O(log n)
- Linear: O(n)
- Linearithmic: O(n log n)
- Quadratic: O(n²)
- Expontential: O(2^n)
- Factorial: O(n!)
Below is the list of some of the most used Big O notations and their performance comparisons against different sizes of the input data.
Big O Notation | Computations for 10 elements | Computations for 100 elements | Computations for 1000 elements |
---|---|---|---|
O(1) | 1 | 1 | 1 |
O(log N) | 3 | 6 | 9 |
O(N) | 10 | 100 | 1000 |
O(N log N) | 30 | 600 | 9000 |
O(N^2) | 100 | 10000 | 1000000 |
O(2^N) | 1024 | 1.26e+29 | 1.07e+301 |
O(N!) | 3628800 | 9.3e+157 | 4.02e+2567 |
Clone this repo:
$ git clone https://github.com/akhenda/es6-data-structures-and-algorithms.git
Navigate to the es6-data-structures-and-algorithms
directory:
$ cd es6-data-structures-and-algorithms
Install the required dependencies:
$ yarn install
To run both tests and linting, run:
yarn run validate
To run the tests alone:
yarn run test
To run ESLint:
yarn run lint
...
Copyright (c) 2016 Joseph Akhenda.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.