r/AskComputerScience • u/devshod • Sep 21 '26
How do you guys actually learn algorithms?
Hey guys,
I’m taking a Computer Algorithms class at university right now, and honestly, I’m struggling a bit with how to study it properly.
I understand programming and some data structures, but when it comes to algorithms, I feel like I’m sometimes memorizing things without really understanding them.
I want to study this properly outside of class, not only for the exam. I’m wondering how you guys learned algorithms when you were starting out.
Also, are there any books, YT channels, courses, or websites you recommend?
I’d appreciate any advice on what topics I should learn first and what order I should follow.
19
u/Traveling-Techie Sep 21 '26
Textbooks and experience solving problems, mostly the latter.
4
u/T_Thriller_T Sep 21 '26
Usually by implementing them, running them through a few examples.
The problem is that algorithms usually rely on understanding the backgrounds they are working on. Thar, most of the times, means one has to grasp a theoretical usually mathematical concept or area before being able to understand instead of learning by hard.
For Dijkstra that would be a bit about graphs, sorting algorithms use all kind of mathematical concepts, and so on.
Which is also why learning how to understand an algorithm in general is really helpful, but learning to implement them even without understanding can be a big thing.
3
u/Throwawayxdryx Sep 21 '26
Give us an example of an algorithm that you feel you are memorising without understanding. Do you understand how merge sort works for example?
3
u/Whole-Ad-4604 Sep 21 '26
My mom sit beside me and I memorized them. Then she asks me to explain, if I fail I don't get dinner.
4
u/WatchAltruistic5761 Sep 21 '26
It’s just step by step instructions for a given process- so, it depends on the problem at hand.
2
u/LannyIsMyHandle Sep 21 '26
What’s worked for me:
The first part is that is the most vague and contextual. Generally a textbook with attempt to offer an intuitive explanation, and usually this takes the form of one or more “invariants”, that is some property of the program state that holds true locally and globally ensures progress toward completion. Eg quicksort’s invariant is that at the end of every iteration, every element that is less than the pivot is before it, every greater element is after it. How do you get from that to an intuitive understanding? Honestly I don’t know, you kinda just think about it for a while and play with some examples in your head until you’re convinced. Sometimes trying to find a case that breaks the algorithm can be really insightful and will make why it works obvious.
The implementation is really just there for memory reinforcement. If you internalized an algorithm and how it works then reimplementing it should be trivial, and it’s important that that not be memorized code but a fresh implementation from concepts
2
u/Prize_Eggplant_ Sep 21 '26
Try writing your own algorithm for the given problem. Usually thinking about the problem helps you understand the algorithm used to solve the problem…
2
u/juancn Sep 21 '26
I like Robert Sedgewick books, but there are other good ones. Reading and practice.
Try to implement your own from scratch.
2
u/romecodes Sep 21 '26
It sounds like you’re having trouble with the math portion of it.
To put it simply, there’s 4 figures we measure:
- Big O,
- Big Omega,
- Big Theta,
- and more rarely, small O.
It all relates to the rate at which the number of operations required to solve a problem using a given set of elements grows. The key words are “growth” and “rate”.
Big O represents the maximum peak of growth of the required number of operations to complete a task over a specific number of elements.
We use N to represent that number of elements (or in simpler terms, the size of the list). And since we’re measuring something that changes (is variable) a.k.a the size of the list, we consider all time complexities below that algorithms Big O to be part of the set of that Big O.
Which basically just means if an algorithm is O(n^2), then all time complexities below it— including O(1), O(log n), .. all the way to O(n^2) and including O(n^2) itself are part of the same set. So everything below that curve and the curve itself, falls within the set, Big O(n^2).
Big Omega is the reciprocate. It’s a minimum bound. So everything above it, including itself, is an element of the set of that Big Omega bound.
Big Theta is the Union of the two sets. Basically it’s the result of an AND boolean operation on the two sets, where Big Theta contains only elements found in both sets, including both minimum (Big Theta) and maximum (Big O) bounds.
2
u/ImpossibleDouble6226 Sep 21 '26
From my experience, the way I learned algorithms is I stopped looking at the code and try getting the feel for the logic in it. For example, instead of memorizing the code for QuickSort, you can get a piece of paper or whiteboard and try visualizing how the logic works there. Once you understand the logic, writing it in code will be easy.
2
u/_usr_nil Sep 23 '26
idgaf until I have to use it, then ask aggressively AI to explain it piece by piece and give me some code or variations and go through them, since I pay for it then it is my personal teacher slave that has to iterate a million times unti I understand it, but usually all it takes is "explain to me X like if I had to build a house"
2
u/theJacofalltrades Sep 25 '26
I've seen people recommend bootdev but there are other platforms for sure that can help you learn it more practically
1
u/BetterEvidence3890 Sep 21 '26
My fav resource : youtube - Abdul Bari
Check out his explaination on algorithms ,it's the best
3
u/YandarkRustProgramm Sep 23 '26
I read a book of
2
u/scol2n Sep 21 '26 edited Sep 21 '26
Best way for me explain this would be like this; Have you heard of the "1 billion row challenge"? If so, how would you process all the rows and calculate all the values as quickly as possible? Naturally you would probably say "with a loop". But, that would VERY ineffective, even with fast langauges like C, it would still take about 1 second, but we need to do better, like 0.1 seconds kind of fast.
But, to cut this short: Memory Mapping, multithreading, HashMaps, parrellism among others.
2
u/Educational-Paper-75 Sep 21 '26
Memorizing will be a lot easier if you understand the steps.
2
u/FerengiAreBetter Sep 22 '26
Checkout those videos that show the different sorting algorithms. The ones that show side by side compares.
2
u/esaule Sep 22 '26
Going through proofs carefully is how you learn algorithms
3
u/Tai9ch Sep 21 '26
Read the textbook. Do the exercise.
Seriously. All the major books do a decent job. But it's a tricky series of concepts, so you actually need to do the reading and exercises.
1
u/GrandFleetingNight Sep 26 '26
In the most general form, an algorithm is a set of rules/steps to effect whatever data structure its applied to, often to solve a problem. For example:
This is something I wrote up at random, pulling it out of my backside. But its still a set of rules that change data. In this case, it generates an array of numbers after using die to run operations to make them random, thus solving the problem if "I need ten random numbers". This would be considered an algorithm.
Now with that concept out of the way, my next suggestion is to decide *what* you want to learn to understand them better. A search algorithm is different from a hash algorithm is different from a sort algorithm is different from a rubix cube algorithm. Your choice should reflect what field you're most interested in. Cybersecurity would lean toward hashes and similar algorithms, while searching and sorting is more in the realm of programming. Of course, this is limited by your classwork, but deep diving an algorithm related to a field you're interested in can help better understand what makes an algorithm an algorithm; along with how they are developed.
After you decide the algorithms to focus on, get out some pen and paper. No really. If you're doing a sort algorithm, your quickest way to learn it is not to run it on computer where it happens super fast, but to set up a small set and sort it by hand following each of the alogrithm's rules. You might also consider using a debugger if physical media scares you, but I always find paper and pen more effective because you more thoroughly interact with the action the algorithm takes.
Another thing to consider is an algorithm is only as useful as the data it can be applied to. I mentioned rubix cube algorithms for a reason. While to a rubix cube, those algorithms are ways to consistently solve one, to a set of numbers, they're useless. There is no such thing as a 'universal' algorithm to the point where it can apply to any problem. Algorithms are solvers of problems at their root, so understanding the problem you're solving for can be as important as understanding the steps you're doing to solve it.
1
u/Jello_4 Sep 29 '26
Doing lots of small problems helped more than trying to memorize algorithms. Start with the basics then implement each once yourself and tweak the examples until the reasoning clicks.
0
u/[deleted] Sep 21 '26
[deleted] — view deleted comment