views:

358

answers:

7

Do you memorize algorithms such as binary search / quick sort / whatever. If so, do you have any tricks for doing so?

A: 

I would never risk trying to memorize these - always try to find a library function which handles it.

James McLeod
There is some middle ground between , 'don't memorize' and 'use a library function', such as 'look at a reference as you implement'.
instanceofTom
+4  A: 

Hi

Its always better to find ways to understand the underlying principle of such algorithms rather than memorizing them. For example - Quick Sort uses Divide and Conquer paradigm (it is a design technique to solve a certain class of problems).

Wiki is a very good starting point to dissect a new topic. You can dig deep by watching video lectures (found this good post on Video Lectures here on SO) and other specific material such as concerned research papers.

Working out examples will also clarify the algorithm better.

I hope this helps.

cheers

Andriyev
+1: Divide and Conquer: a fundamental Design pattern for many algorithms.
S.Lott
+4  A: 

I don't memorize them in the sense of rote memorization of code or pseucode, but I could always code binary search and quicksort from memory because I know how the logic works. This comes from studying algorithms, which I absolutely recommend.

danben
+2  A: 

Seek to comprehend instead of memorizing. I find it helps me to work through proofs of an algorithm to get the inner workings of what's going on, and that's usually proven to be good mnemonic strategy.

John Feminella
+1  A: 

No, that is what the Internet is for. I'd rather demand page that kind of knowledge in, rather than have it take up space for something more useful.

Bryan Batchelder
+3  A: 

I think that it is worthwhile remembering the concepts behind important algorithms and data structures. But for the implementation side, I would argue to use a library function if at all possible. It is easy to forget about borderline cases when you write down the algorithm - and it is quite probable that you don't think of them if you do a unit test. As a result, even such simple algorithms as binary search are likely to be broken:

I remember vividly Jon Bentley's first Algorithms lecture at CMU, where he asked all of us incoming Ph.D. students to write a binary search, and then dissected one of our implementations in front of the class. Of course it was broken, as were most of our implementations.

From the Google Research blog. The rest of the text is a recommendation worth as well, but it's not about memorizing algorithms.

nd
A: 

You should know the general principles behind these algorithms because they are the fundamentals of programming and it will give you insight into where they should be used. For example, you should understand quick sort well enough to understand why it can be O(N^2) in pathological cases.

However, there is absolutely no point in remembering enough detail to be able to code production quality implementations of these algorithms on the first try off the top of your head. This is what libraries are for. For example, if all you remember is the canonical 3-line version of the quick sort and you can't remember how to implement one that doesn't easily find the O(N^2) cases and sorts in place, there's nothing wrong with that.

dsimcha