views:

779

answers:

9

I want to implement algorithms and data structures for practice. I have a bunch of textbooks I am working through and although I can understand the idea and theories I can never seem to close the book and implement working code on my own, like people do in ACM programming contests.

I can learn how to write a linked list in C, and forget how to do it two hours later. If I use a higher level language like Python, then I have problems mapping psuedocode because some constructs like pointers don't exist. On the other hand something like C is very complicated and I constantly make mistakes.

I really want to improve my algorithm skills but I feel like I'm never getting any better. Algorithms are important but they're so hard! I must be doing something wrong. I spend a lot of time studying but the concepts never seem to stick. When I compare myself to other contests on the live scoreboards, they're often 10x faster than I am. Any advice?

+7  A: 

Try figuring out the problems posted on Project Euler. They start off pretty easy and can be good practice. It starts off fairly easy but it gets difficult very quickly. You will need to implement good algorithms to solve them. I would recommend Googling some of the problems (Some tend to be variations of common problems) and then try to implement the algorithms you find by yourself, and then checking the reference to make sure you have it correct.

I hope this will help you get into the right mindset when thinking about algorithms. It is not important to memorize them, it is much more efficient to determine what train of thought you need to take to figure them out.

Just my $0.02

CookieOfFortune
an alternative would be TopCoder
jdt141
+1  A: 

It just takes practice. The first battle is to know what algorithms exist and when you might use them. When it comes time to implement a solution, figure out what algorithm may work, then google it. (This applies to design patterns too [e.g. Decorator, Singleton, Facade, etc..] )

I doubt many developers can write a heap sort from memory.. They know what it is and its advantages over other sorting algorithms, but the actual implementation can easily be gleaned from the internet on demand.

Ryan Emerle
If you write a heapsort, you're not writing it "from memory." You're figuring it out based on your understanding of the algorithm. If you understand the algorithm, then you can write it. If you can't write it, then that implies there's something you don't understand or don't know about it.
mquander
And your "understanding of the algorithm" is not "from memory"? I didn't imply any kind of photographic memory :) I think you might have gotten lost in semantics. (Who memorizes code without understanding it?)
Ryan Emerle
Well, I know, sorry if I took it the wrong way. I might have jumped all over that one a bit quick. Anyway, you're probably right that most developers couldn't tell you much about a heapsort.
mquander
Not heapsort, I've never implemented one. But I could bore you on the subject of quicksort, if ever called upon at an unimaginably dull dinner party. I'd guess that everyone knows some standard algorithms but not others. Those were the days, jumpers for pivots, pseudo-median of 9. Isn't it?
Steve Jessop
+1  A: 

Algorithms are just a way of doing something. In that spirit (and probably to try to sell as many copies of their book as possible) authors tend to use pseudo-code to generalize an algorithm and leave it to the learner to actually implement it. That's great if you already know a language and can implement an idea into that language, but not so great if, while you're trying to learn a new algorithm, you're also trying to learn the language to implement it in.

I would suggest sticking to a language that you are comfortable with. Once you've gotten to that point, pick up an algorithm book with examples in that language. Once you've gotten the hang of it in something you're familiar with, it should be a little easier taking the knowledge you've gained to a language you're unfamiliar with and implementing it there as well.

Michael Todd
+1  A: 

http://train.usaco.org/ is a very good free training program for programming contests and algorithms.

zak23
+8  A: 

Ask yourself this question: despite the inherent difficulties you are encountering, are you enjoying programming/learning CS? If not, then you might want to think about whether this is really something you want to pursue in the long term. I'm not trying to discourage you, but rather trying to possibly save your some headache & heartache.

CS is a tough field. There are certainly many people who are "born" programmers, and everything comes easy to them. I wasn't one of these people- I had to struggle really hard in my CS classes. On the other hand, I loved programming, and even though it was often difficult to implement algorithms assigned in class, it was still fun for me. If it's not fun for you the majority of the time, you might want to reconsider being in this for the long haul.

If you find that you do in fact enjoy it, don't worry about people who can code 10x faster than you; that will come in due time. Concentrate on implementing the algorithms correctly rather than quickly; you'll develop speed over time as you gain more experience and learn the languages/APIs better. I would also caution you against reading contest code for good examples of algorithms, unless the contests are also judged on quality, conciseness & readability. You can really learn a lot from reading others' code, but make sure the material you are learning from isn't just a quick hack.

One final recommendation- try to implement in your language of choice an algorithm for which you already have the source code, but in a different language. E.g. if you have the source to Quicksort in Java, but know Python well, try re-coding the algorithm in Python from the Java source. This will help you get a feel for what the algorithm is actually doing, while still having some training wheels on.

Caffeine Coma
+1  A: 

Algorithms are just a way of doing something and data structures are just a way of quantifying something.

I guess it depends on your learning style, but when I was in school it helped me to think of analogies for these things. Like, maybe a bubble sorting algorithm is in some way like a game of hopscotch, stacks are like PEZ dispensers, doubly-linked lists are like bureaucrats with telephones, etc.

Take your time and move at your own pace. Don't worry about how other people are doing. Stay on top of your own game.

hypoxide
+2  A: 

I recommend instead of trying to memorize the code, which is what I assume you are trying to do, learn how the data structures and algorithms work. That way you can implement them in any language with only a very basic understanding of the syntax.

Ryan Nielson
+1  A: 
  • Spell out the algorithms in natural language. Try to grasp the idea behind it.

  • After you have read about an algorithm and think that you got it, close the book, take a blank sheet of paper, and write the algorithm down, in your own words. This is a very important learning technique.

  • Sometimes it may be helpful to think about programming in a different way. You could try to read "Structure and Interpretation of Computer Programs" for a different viewpoint. This may also help with understanding higher level languages like Python.

Svante
+1  A: 

You mentioned having trouble with C and Python. Python is object oriented and C is not. That is one thing that you must learn and after you have learned that you will find that implementing a linked list is almost the same as in Python you create a new object (and instance of a class - in this case it will be a node) and in C you will have a struct which does something similar instead it won't be of a class but a some sort of data type (I don't remember correctly for the name). Then learn what is a necessity to produce something of that sort (i.e. a singly linked list node requires one piece of information, a piece of information telling you what the next node is).

I think grasping a computer language is what you need to do first before actually trying to implement the algorithm or data structure from your textbook. Try out simple exercises to learn the language.

Anyone can correct if I'm wrong - I'm just learning myself too :).

wchan