收藏 分享(赏)

five algorithms you must know.doc

上传人:hyngb9260 文档编号:6751346 上传时间:2019-04-22 格式:DOC 页数:3 大小:41.50KB
下载 相关 举报
five algorithms you must know.doc_第1页
第1页 / 共3页
five algorithms you must know.doc_第2页
第2页 / 共3页
five algorithms you must know.doc_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

1、5 algorithms you must know(2007-11-14 18:58)分类: C语言相关Sorting - The most fundamental thing in algorithms, and its almost a given. (And it means the same in English as it does in Math, so no explanations needed, I hope.) There are tons of crazy sorts out there, but quick sort is the one everyone shoul

2、d be familiar with, merge sort is the one that comes in handy when you cant fit everything into RAM, and introspective sort is important to learn, because it teaches us an important paradigm - you dont need to find an algorithm that does everything the best - use the best algorithm for each test cas

3、e!Real World Application: Youre in a library, given a bunch of books. You want be able to look for books without looking at every book every time. Sort them and do quick checks!Binary Search - This is the classic example of Divide and Conquer. Remember that game 20 questions? You have to ask 20 ques

4、tions to guess a word. Well, the way to cheat is to narrow it down half way every time. The English Dictionary has about 500,000 words, meaning you would still have queries to spare.Yes, it would be boring, but it would go something like this:Does your word come before the word “marry“? YesDoes your

5、 word come before the word “gerrymander“?You get the point.Assuming you already know you have to sort, you can binary search at O(log_2 N). Which for say, a trillion, is less than 50.But its application doesnt stop there, as it (as well as its cousin ternary search) can help you solve numerical equa

6、tions (this is actually call bisection, but same paradigm). For example, you want to find the cube root of N (N(1/3), but dont have some sort of library ready. Easy solution? Binary search! You know x=y3, so “guess“ a value for x, and go from there!Real World Application: According to folklore, Prof

7、essor Skiena would demonstrate binary search by asking for a name, and then looking it up in the phonebook by checking the middle of the book, does a comparison, and literally tear out half the book and throw it in the trash. You can probably come up with a less dramatic example.Hashing - Conceptual

8、ly easy, if only because most classes hop over this with idealizations. The legendary Udi Manber was fond of saying, “hashing, hashing, hashing“. No, real hashing is hard, but dont be afraid to use it. Imagine you have a closet full of shirts. Its very hard to find a shirt. So what can you do?Hash i

9、t. Take every shirt, and put it in a different drawer depending on the color of the shirt. Whenever you want a shirt, all you have to do is simply open the drawer with the corresponding color. Thats hashing in a nutshell.Of course, you might (and likely) have more than one shirts of a given color. T

10、hat is hash collision, and there are tons of research papers in the world on the topic, so know that they exist.To paraphrase, no one ever got fired for using a hashtable before!Real Life Application: Gave one describing it above. You can use hashing on almost anything, if you dont need to know the

11、ordering.Dynamic Programming - Sometimes it is known by its other name - Memoization. No, it is not memorization, no r. Fine, its most common name is caching. Save your results so you dont have to do it again!Its not always so dramatic, but how many times do you want to recalculate things? The class

12、ic example of the Fibonacci sequence is an example:f( n ) = f( n - 1 ) + f( n - 2 )Its trivial to translate this recursively. So we do. Unforunately, if you want to know, say, the 100-th Fibonacci number, and your computer does, say, a trillion calculations a second, it will still take, about 11 yea

13、rs to get your answer. So save and reuse your results! If you did do that, even if you only do 100 operation a second, itll take less than a minute!Yes, someones going to point out you only need the last two numbers, so you can use a for loop. Youre perfectly correct, and thats “Dynamic Programming“

14、 (DP). Sometimes memoization is faster, sometimes dp is faster. Experience will tell you which one is which.Im handwaving right now, but this isnt simply “saving down results“, of course. DP/Memo is when you know that certain results will be re-used in the future, exploiting the fact that well, you

15、dont need to redoing the same thing over and over againReal Life Application: You need to multiply say, 5123 x 3333 (yes, its an obvious example) without using calculator etc. Well, you work out that 5123 x 3 = 15369. What do you do? Write it down. Do you have to multiply 5123 x 3 again for the othe

16、r digits? You can if you want, but you saved yourself some work because you wrote it down!Search Algorithm - This is relatively generic. The two most common is Depth-First Search (DFS)and Breadth-First Search (BFS). Understanding these leads to you more exotic things like A* Search, and its a fundam

17、ental exploring algorithm. Too many things can be reduced into a graph, and knowing the best way to search a graph will come in handy. DFS is basically a stack-based implementation, while BFS is a queue-based implementation, and each with its strength and weaknesses.Real Life Application: You have t

18、o get from point A to point B. You dont know if you can get there. So what do you do? You drive and drive around until you hope to find the destination. Reach a deadend? Turn around and come back where you came from. It might not be the shortest way, but if you tried every possible road, youll know

19、that maybe you cant get to China from New York. Whoops. (And thats a DFS!)When people think of algorithms, there will be people who will keep saying how rarely they use them in the “real world“. Its true. If youre a codemonkey that codes exactly to perfect specs everytime, you might never come acros

20、s such a need. Knowing algorithms and what to do the rare times you need to is a valuable tool. Besides, its about learning the tools and different ways to think and attack the same problem. Ultimately, anyone can look it up in a textbook or reference book, but understanding algorithms will allow you to solve problems that arent as flexible as the textbook.So read up, good luck, and maybe actually implement them!

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报