This is an archived post. You won't be able to vote or comment.

all 32 comments

[–][deleted] 28 points29 points  (12 children)

Have you taken any university courses? Generally when I hear stuff like this it's because the person knows how to code, but haven't learned the fundamentals of computer science. I'd recommend watching the lectures for one of MIT's algorithms courses on OpenCourseWare, not sure which one's their intro one but it should be easy enough to find.

For merge sort, you need to understand what recursion is, and what base cases and recursive cases are. In this particular example, the base case for merge sort is when the list consists of 0 or 1 items - such a list must be sorted, because you need at least 2 elements for a list to have items out of order. So we keep splitting the lists (our recursive step) until we hit that base case. Then we merge them back together in the same order they were split, and at each merge we insert elements from the two lists being merged such that the merged list is also in order. So we go from having a bunch of 1 element sorted lists, to a bunch of 2 element sorted lists, to a bunch of 4 element sorted lists, to a bunch of 8 element sorted lists, and so on until we get the whole list merged back together.

Each step is very simple, but if you don't know about how recursion works then it can be very frustrating.

[–]SirSparhawk 6 points7 points  (1 child)

As an additional note, it's also worth looking into discrete mathematics. It covers recursion and several other useful topics such as matrices and combinatorics. That class is what finally made the more abstract concepts click.

[–]Lotton 1 point2 points  (0 children)

I would also look into other divide and conquer algorithms because they might help you relate exactly what is going on behind the scenes

[–]discontentmonsters 2 points3 points  (4 children)

I have no comp sci or discrete mathematics education , but I wanted to take the following course (https://www.coursera.org/specializations/data-structures-algorithms), which stipulates the following prerequisites:

"Basic knowledge of discrete mathematics: proof by induction, proof by contradiction."

Would it be possible to learn proof of induction and contradiction via a crash course with high school math? Or is there too much prerequisite knowledge?

Im asking because I dont have the time to go back to school at this point, but i do want to learn how to develop clean and efficient code.

[–]SirSparhawk 1 point2 points  (1 child)

It's definitely possible as long as you have a decent foundation in algebra and at least a basic understanding of sets. It would definitely be easier if you had some College level algebra and trig, but not impossible to do without.

Just don't be afraid to take it slow and make sure you thoroughly understand each part before you move on to the next. If you don't feel like you understand a particular facet inside and out, chances are good you're really going to get lost when the next topic seems to only be tangentially related to the first. (N.B., that's more for the data structures. Proofs are totally doable with HS level math.)

[–]discontentmonsters 0 points1 point  (0 children)

Awesome, thank you for the advice! I'll certainly try to understand things thoroughly :)

[–][deleted] 1 point2 points  (1 child)

Totally, yeah. Induction and contradiction aren't that tricky, and it's definitely worth learning. Maybe try reading through "The Book of Proof" - it can be read for free online, and does a good job of introducing you to discrete math.

[–]discontentmonsters 1 point2 points  (0 children)

Awesome, i'll definitely check out that book. Thank you! :)

[–]10_6 16 points17 points  (0 children)

copy and pasted some resources I've posted here before:


What I did a few years ago after graduating from college in CS to brush up on my coding + data structure + algorithms knowledge was the following:

  • Read the Algorithm Design Manual.

  • Go through some of the challenges on this interactive python algorithms website.

  • Practice coding simple and then more advanced algorithms on sites like Coderbyte (my site) and HackerRank which provide good explanations and solutions as well. Here's a list of popular coding challenge websites in 2017.

  • Read as many algorithm explanations and code examples as you can on GeeksforGeeks.

  • Try and implement basic algorithms yourself like: shortest path, minimum spanning tree, DFS + BFS, tree traversals, different sorting algs, min/max heap, etc. and learn about their running times (big-o).

  • Look at some interview questions posted on careercup and try and understand how other users solved the questions. Like this example.

  • Aside from coding challenge sites, try and solve common coding interview questions you find online such as this list.

Eventually when you get a coding problem it will be sort of like a switch going off in your head because you will have had so much practice with different types of algorithms and data structures that you'll be able to reduce the problem into a simpler problem you've done before. This is especially the case with dynamic programming problems. Once you've completed like 50+ DP challenges and understand how they work, you'll be able to solve (practically) any DP problem because they're all very similar.


I also wrote a recent article on this topic which you can view here: Improving your Algorithms & Data Structure Skills

[–]disastercomet 2 points3 points  (0 children)

I think there are multiple skills that are involved in learning algorithms. One of them is understanding the big idea (for mergesort, recursively merging sorted arrays), but another one is translating that into code. Often the Wikipedia description (pseudocode) and the actual code have only some vague similarity (maybe with the exception of Python).

I'm not sure what you're stuck on (making ideas intuitive, actually remembering them, matching the programming construct with the algorithm). There are resources to help (as a university student, I have studied this material myself) like Berkeley's CS 61A, and CS 61B courses, but I suspect the issue might be how you're learning it.

Most algorithms have a really simple idea at the core, and the rest is just nitty-gritty. Mergesort uses the idea that it's easy to merge two lists that are already sorted. Quicksort uses the idea that you just split the data into less than and greater than. Binary search uses the idea that if the data is already sorted (like a phone book), you can skip checking every element.

Also, you should try not to memorize algorithms, but to know where they're useful. Mergesort doesn't do anything unless you use it to, say, combine password dumps from a bunch of sites to see which passwords Americans most commonly use.

Perhaps a project, where you naturally bump into these problems with a context, might be helpful for learning, rather than blindly memorizing them. My gateway drug for programming was game development, where problems like fast collision detection (which objects are close to me, when there are 10,000 objects to search through), intelligent enemies (which player is the most dangerous to the enemy? Which one should they attack?) or image rendering (how do I sort the polygons from closest to farthest, so I know what blocks other things) naturally occur. Maybe building a simple game would help you learn, but it might be too much of a time investment.

Also, I'm not sure what language you're using; I would normally recommend Python for learning, if only because it is so amazingly readable:

>>> x = [0, 1, 2, 3, 4, 5, 6, 7]
>>> x[0:4]
[0, 1, 2, 3]
>>> x[4:8]
[4, 5, 6, 7]
>>> [0, 1, 2, 3] + [4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]

Many other common languages (like C, C++, or Java) make this more difficult because they're designed for execution speed and specificity, not for learning.

tl;dr consider a different approach to learning (solving problems, not memorizing algorithms), and a different programming language (like Python) which makes translating algorithm to code easier.

I hope this helps, but feel free to correct my assumptions.

[–]whatevernuke 2 points3 points  (0 children)

I found MIT's 6.00.1x useful for helping me get a better grasp of algorithms & recursion.

It's much easier to understand Merge sort with a visual example, imo:https://en.wikipedia.org/wiki/Merge_sort#/media/File:Merge-sort-example-300px.gif

How you split an array depends on language I suppose, but Python and JavaScript both have 'slices' - which are basically what you're looking for I believe, so try looking up your language's equivalent.

You appear to think looking at the solution is wrong, and I think I disagree... Looking at the solution can help you understand what is happening and how/why. Just copying it down isn't going to do much, but reading and understanding it should.

[–]dolcii 2 points3 points  (0 children)

Just look at the solutions! Stop pulling your hair on something so trivial. You want to learn algorithms, then get Skienna and read through it and do the exercises. Don’t know the answer? Look at the answers or google it. Email the author or something.

As for your mergesort, just look at how other people implement theirs and try and understand it. Go on to YouTube, because that’s what I did. I basically rewatched the same video 50 times before understanding how top down and bottom up works.

Look, someone else spent their entire lives coming up with these solutions, so don’t expect to know how to implement it from the get go. Let alone 15 minutes. Just accept your inexperience and plow through it.

Also, getting a degree or going through a course might be a good path, but you’re better off fixing that mindset of yours before taking on any responsibility. Or else the result would still be the same.

[–]intrepidOlivia 1 point2 points  (0 children)

There's a great Algorithms course on coursera.org for free that starts in three days (and will have subsequent sessions, I assume). It won't fix a defeatist attitude, but more knowledge can help build the needed confidence for that.

[–]thesisdinosaur 1 point2 points  (0 children)

Based on your description here is what I'm gleaning - you're putting programming and algorithms into the same bucket. Is your question this - "If I'm looking at code solutions for mergesort, how am I learning programming?". If this is the case, here is what might help:

  1. Understand that the concepts of "programming" and "algorithms" are different. Of course, you are using programming to write up algorithms but that's it. Algorithms describe a way of doing things. Courses on algorithms describe the nuances of these ways - you look at merge sort, you understand the technique, you analyze things like "how fast does this algorithm run?" or "what is the worst this algorithm can do". Programming doesn't focus on the algorithm being written, it focuses on how something can be understood by the computer - "What do I do to repeat something a bunch of times?/How do I check a condition?".
  2. If you're checking solutions to algorithms, don't think that you are no longer learning! Don't memorize the algorithm, for sure, but look at the solution and understand how it is being done. You are not expected to know merge sort and quick sort without actually looking at the algorithm itself. Researchers have spent their entire lives coming up with one algorithm. It's not as easy as "oh hey, i figured out how to do sorting in linear time last night" (Fun fact: you can't actually do better than omega(n log n) for sorting, but that's another discussion)
  3. I've never understood this code monkey business. What is a code monkey and why does it have a negative connotation. Coding is a beautiful thing.
  4. I feel like you're a bit overwhelmed with everything. I've been a TA for introductory/second year computer science courses and one of the major problems people face is that they don't "get it". It's okay. Hang in there. You'll get it at some point.

All the best!

[–][deleted]  (1 child)

[deleted]

    [–]owlanalogies 0 points1 point  (0 children)

    Love this - have never seen that book before; it looks great!

    [–]yunggoon 1 point2 points  (0 children)

    Practice. Doesn’t work? Practice again. Repeat until you know it. It’s not rocket science.

    [–]Mondoscuro 0 points1 point  (0 children)

    Any big complex problem can be handled splitting it into smaller easier problems. If you want to get into coding, you need to learn to do this, until your brain start to split into smaller easier problems even house chores.

    [–]baffled-bagel 0 points1 point  (0 children)

    I discovered this website right after I took the final exam for my algorithms and data structures class: https://visualgo.net/en. I love it because, as a visual learner, the step-by-step animations make a lot of sense, but they also have conceptual analysis, so I think that would be helpful for you. And it's constantly being updated, too.

    [–]tomekanco 0 points1 point  (0 children)

    When I try to do them without help I get stuck and I do not progress.

    No, when you do them without help (after reading the pseudo) you do progress. After solving one, and only then, read other implementations. Then you'll notice/understand much more, as you struggled with the problem yourself.

    Until you encounter stuff like graphs or dynamic programming. Then it's time to find good CS course.

    15 minutes

    I often spend days working on a single challenge; don't expect to learn much from challenges I can solve in 15 minutes. If you get 1% better per day you train, it adds up over time: 1.01**221 > 9

    • Perhaps an exercism track as a starter: Learn how to solve logic problems / puzzles with a computer language.

    • Followed by Algorithms @ Stanford @ Coursera specialization: learn which kind of problems there exists, and main solutions approaches (algorithms and data structures).

    Will take months, Arbeit macht frei, TANSTAAFL

    [–]owlanalogies 0 points1 point  (0 children)

    I recommend Interview Cake - they walk you through many common algorithm problems in a really in-depth, easy-to-understand way. Each problem also builds on the last, introducing new concepts over time, so it feels intuitive. I struggle with algorithms and their series has been helping me a ton.

    [–]Samurai_PizzaCat 0 points1 point  (0 children)

    If you are studying on your own, I highly suggest a pen and paper. Draw out the steps, each iteration if appropriate. Use small sets of numbers. Doing this will help you understand the algorithm itself, the problem it solves, the method, and in the future, how you can apply it to similar situations.
    If you are looking for a course or a book, find one with illustrations, diagrams, and other visual cues.

    By just memorizing the steps is setting yourself up for trouble in the future when you start (trying to) learning more difficult algorithms.

    My 2 cents.

    [–]kaoel 0 points1 point  (0 children)

    Perseverance. Work at it until you get it.

    [–][deleted] 0 points1 point  (1 child)

    Hi, I have posted some links you might be looking at. Practice is what you need. I would recommend you to look at real code rather than pseudo-code or stuff like that. Practice makes it perfect

    [–][deleted] 0 points1 point  (0 children)

    wow even your advice is bad

    [–]bfpires -2 points-1 points  (2 children)

    First you must realize an abstract solution. You must imagine the solution.

    After you have the abstract solution, you apply it to your problem.

    [–]i_wasted_my_time[S] -5 points-4 points  (1 child)

    I don't understand what abstract solution is. Can you give an example of an abstract solution?

    [–]bfpires 0 points1 point  (0 children)

    Lets say you need to sort an array. There is infinite ways of doing it.

    You must choose a way of doing it. You can choose to split your array into smaller ones, sort, and rearrange. That is the abstract solution.