What’s an algorithm? 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. This seems pretty easy to understand, right? However, until you’ve practiced on repeat, algorithms can be tricky to really understand. Solving problems with an algorithm helps us to break it down into smaller, easier to tackle pieces.
Here is a good visual for breaking down the pieces and characteristics of an algorithm:
There are some commonly used types of algorithms in JavaScript:
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 a solution.
Recursive Algorithms - This follows a loop, in which we follow a pattern of the possible cases to obtain a solution.
And when practicing solving algorithms another concept to keep in mind is time and space complexity. What? Big O Notation, that's what.
Time Factor: Time is measured by counting the number of key operations such as comparisons in the sorting algorithm.
Space Factor: Space is measured by counting the maximum memory space required by the algorithm.
Space Complexity: Space complexity of an algorithm refers to the amount of memory that this algorithm requires to execute and get the result. This can be for inputs, temporary operations, or outputs.
Time Complexity: Time complexity of an algorithm refers to the amount of time that this algorithm requires to execute and get the result. This can be for normal operations, conditional if-else statements, loop statements, etc.
There is so much more to talk about with algorithms and even more with Big O Notation. I think this may be more than just two parts. Already you should see how important data structures and algorithms concepts are to have at least a general understanding of. And if space and time complexities make your head want to explode, you're not alone! Still important to have some idea of how much space and time your solution will require and being able to think about how to downsize those needs, you’re well on your way to grasping these concepts. Thanks for reading!
Top comments (0)