Ask HN: How should I study algorithms and datastructures?
Dear all, I am currently studying towards a Bachelor's degree in Computer Science in Sweden. Right now I am attending a course called "Algorithms and datastructures", where we get an introduction to sorting, searching, priority queues etc.
If feel that I need to spend a lot of time in order to really understand the essence of the algorithms, which gets very time consuming when you need two days only to understand insertion sort and merge sort.
How do I study these topics the most efficiently? How well should I know certain algorithms and datastructures?
10 comments
[ 2.7 ms ] story [ 31.9 ms ] threadLong answer:
The Algorithms and Data Structures course is one of the foundational courses of a CS degree. Even if you think you don't need to be able to implement the algorithms you learn in your work later on, it is important to have some idea of which algorithms exist for a given task.
Also, and perhaps even more importantly, understanding how these algorithms work really hones your problem solving skills. You learn how to abstract the information given to you as part of a problem into an appropriate data structure, and what kind of operations you can carry out with which data structures. You learn how to cope with several different layers of abstraction and develop an intuition for programming approaches that are fast and/or light on memory. Basically, the ADS course doesn't teach you how to be a programmer, but how to think like one.
As for studying for this course: there's nothing like doing it yourself. Chances are, you already have to do so anyway as part of your course work. Take the programming tasks seriously and really do try to do it yourself. (Get friends to help you by all means, but don't let them do the work for you.)
I would advise you not to slack either: based on your information and the ADS course at my uni, I'm guessing that you're still at the very beginning of your degree. It isn't going to get easier in terms of workload, and the algorithms you learn about later on in the course are a lot more complicated than those at the beginning. But if you've made the effort to understand the first bunch of algorithms, you'll be in good shape to understand the latter ones too. So yes, it is hard work, but you can do it :-)
However if you still want to study them, there are plenty of resources online, like this youtube channel[1].
Taking 2 days to understand insertion sort is fine but you just need to practice learning more often and use the things you learn more often. Even if it's on toy projects.
https://www.youtube.com/user/mycodeschool
1. Merge sort. Given two sorted sequences, they can be merged in linear time. Given an algorithm that does so, we can sort a list in O(n log n) time, as follows: split the list into two equal halves, merge sort each half, and then merge the result. (The base case is that sorting lists of length 1 is really easy.)
2. Insertion sort. Let's say you want to sort in increasing order. To make things concrete, let's say your given list is
[3, 1, 4, -1, 5, 9, 2, 6, -5, -3]
You start by walking through the list: 3, 1, ... WAIT. That's not right, 1 should come before 3. Let's drag it to the front of the list where it belongs. We now have:
[1, 3, 4, -1, 5, 9, 2, 6, -5, -3]
Now we again walk through the list: 1, 3, 4, -1 ... WAIT. What's -1 doing here, let's drag it to the front of the line:
[-1, 1, 3, 4, 5, 9, 2, 6, -5, -3]
Again: -1, 1, 3, 4, 5, 9, 2, ... WAIT. What's 2 doing here, we need to drag it forward, but not all the way to the front:
[-1, 1, 2, 3, 4, 5, 9, 6, -5, -3]
And so on. Is this what you meant by essence?
https://visualgo.net/en/sorting
When starting out you'll be learning, for example, the different sorting algorithms in great detail. This isn't terribly useful later on (the level of detail at least). What is useful is that they each have a shape, a style of functioning. Merge sort as one of the quintessential divide-and-conquer algorithms provides an excellent template for other algorithms with the same shape, but meant to compute something different. While bubble sort is a terrible sort algorithm, the pattern of bubble sort for computation is present in numerous algorithms. Same with insertion sort and shell sort and the rest.
It's like language acquisition, or at least like language acquisition is for me. When learning Spanish, I learned a lot of specific instances (hablo, hablas, habla, hablamos, hablan as 5 distinct things). Then I learned the pattern (grammar rules, -ar verbs generally drop the -ar and -o means I <verb>, -as means you <verb>, etc.), then I focused on root words in vocabulary (hablar means to speak) rather than memorizing every conjugated form of a verb.
Same as physics and calculus. Learn specific cases at the start, then you learn the rules, then you apply the rules to new forms to construct novel (to you) solutions.
And as others said, code. My algorithms class didn't require much programming, technically, it was rather high level. But I coded everything I could to try things out and understand them. Sitting down with pencil and paper to understand it was sometimes helpful, but the act of coding was more effective.
EDIT: High level in that it was focused a lot on the maths of algorithm analysis. I had one earlier in college that was more practically focused, on implementing algorithms, but the one that really stuck with me was the later one.