Memorizing important algorithms such as binary sea

2020-06-08 13:16发布

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

标签: algorithm
7条回答
叛逆
2楼-- · 2020-06-08 13:51

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.

查看更多
时光不老,我们不散
3楼-- · 2020-06-08 13:51

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

查看更多
仙女界的扛把子
4楼-- · 2020-06-08 14:03

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.

查看更多
我只想做你的唯一
5楼-- · 2020-06-08 14:08

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.

查看更多
家丑人穷心不美
6楼-- · 2020-06-08 14:09

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.

查看更多
时光不老,我们不散
7楼-- · 2020-06-08 14:11

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

查看更多
登录 后发表回答