Algorithms and Data Structures in JavaScript
Oleksii Trekhleb γ»3 min read
Hello Readers! Iβve recently launched JavaScript Algorithms and Data Structures repository on GitHub with collection of classic algorithms and data-structures implemented in ES6 JavaScript with explanations and links to further readings and YouTube videos. There is also Algorithms and Data Structures YouTube playlist that contains all the videos mentioned in that repository so you may just go and take this hand-made online learning course :)
So I guess youβve already grasp main idea of the projectβββhelping developers to learn and practice algorithms and do it in JavaScript.
To make this process even smoother Iβve tried to put some graphical illustrations for each algorithm and data structure where it was possible just to make the idea behind those algorithms to be easily grasped and memorized.
You also may find some practical information just in the root README file that may be handy while youβre studying. Such information as:
- big O notation graphsβββto see quickly what is worse O(n!) or, letβs say, O(nΒ²)
- list of some of the most used Big O notations and their performance comparisonsβββto get the idea how big is 10! (it is 3628800)
- data structures operations complexityβββhow fast are searches, reads or insertions for different data-structures
- comparative table of sorting algorithms complexityβββto help you choose proper sorting algorithm in your situation
All code is 100% covered with tests. This is done not only to keep code working correctly but also to give you an illustration of how each algorithms or data structure works, what basic operations they have (letβs say polling for heap) and what are the edge cases (what to do if graph is directed).
Repository also has a playground. This is just small function template along with empty test case that will help you to start testing or working on algorithms just right after cloning the repo.
Currently there are following data structures a covered:
- Linked List
- Queue
- Stack
- Hash Table
- Heap
- Priority Queue
- Trie
- Tree (Binary Search Tree, AVL Tree)
- Graph (both directed and undirected)
- Disjoint Set
On top of that data structures there are more then 50 popular algorithms are implemented. Among them are sorting, searching algorithms, graph/tree/sets/string/math related algorithms. All algorithms are also categorized by theirs paradigms:
- Brute Force Algorithmsβββlook at all the possibilities and selects the best solution
- Greedy Algorithmsβββchoose the best option at the current time, without any consideration for the future
- Divide and Conquer Algorithmsβββdivide the problem into smaller parts and then solve those parts
- Dynamic Programming Algorithmsβββbuild up to a solution using previously found sub-solutions
- Backtracking Algorithmsβββsimilarly to brute force try to generate all possible solutions but each time you generate a solution test if it satisfies all conditions, and only then continue generating subsequent solutions. Otherwise backtrack and go on a different path of finding solution
JavaScript Algorithms and Data Structures repository is still under active development and more algorithms and data-structures are yet to come. And you may also be a part of it by contributing your code and your implementations of web-known algorithms!
I hope this repository will be helpful for you! Enjoy coding!
THIS THIS THIS THIS THIS. Google recruiter reached out to me the other day. We had a conversation and he wants me to be ready for the interview by studying this stuff. The materials he sent honestly. This is going to help me so much. Thank you!
Hey if you got to learn this maybe you might want to consider studying the complexity of a piece of code containing for example multiple for-loops or recursion
O (n^{2)} is not horrible. If you problem is matrix multiplication, the you're a god if you do it in O (n^{2).} The complexity is relative to the problem. Also, time complexity should be considered together with space complexity. If you can sort in O(n) time wih O(n^{2)} it might be not that good, but it depends on the size of your data. For small data you can afford to store O(n^{2).} A good complexity table is the one of "data structure complexity in c++" which compares operation for different data structs
Hi Isaac,
I think the chart was taken from bigocheatsheet.com/ and there it mainly talks about data structure operations (read, insert, update, delete) and sorting algorithms.
I agree with you that to say a complexity is bad cannot be done independently of the problem.
Yeah exactly thats the cheatsheet I had in mind :)
Hi Oleksii!
Thanks for sharing. Where'd you get the image from? It looks a lot like the one on bigocheatsheet.com/ but it's missing the "excellent" part in the graph, although it's present in the legend.
Has anyone tried online courses like this one udemy.com/coding-interview-bootcam... ?
There is Frontend Masters' course frontendmasters.com/courses/data-s... but it's so irritatingly wordy. I like concise and to the point way of teaching.
P.S. You all are probably aware of this repo but in case or for future references - github.com/getify/You-Dont-Know-JS
Wow, this is awesome!
Cooooool! I wrote a post on Linear and Binary Search. I'd love to get your feedback, Oleksii. :)
Conclusion: In CS, no algorithm is "excellent".
- Not even O(1)?
+ No
- That's not fair!
+ No, there are not "fair" algorithms, either.
This is so sick! Thank you so much for this. I wonder if we could do this for typescript?
This is so awesome! Thanks!
Thank
You
Soooo
Muchhh....πππ