欢迎来到算法乐园 / Welcome to Algorithm Wonderland
欢迎来到算法乐园
欢迎来到算法乐园 / Welcome to Algorithm Wonderland
算法就在你身边 / Algorithms Are All Around You
你有没有想过,当你每天早晨起床、刷牙、洗脸、吃早餐的时候,其实你一直在做一件非常有趣的事情——你正在执行一套“指令”,一套解决“如何开始新的一天”这个问题的指令。每当你按照一定的步骤完成某件事情时,你就在使用算法。
Have you ever wondered that when you wake up every morning, brush your teeth, wash your face, and eat breakfast, you are actually doing something very interesting—you are executing a set of "instructions," a set of instructions to solve the problem of "how to start a new day." Every time you complete something following certain steps, you are using an algorithm.
让我们来做一个小实验。想象一下,现在是早上七点,你需要穿好衣服去上学。请你闭上眼睛,在脑海中仔细回忆一下你穿衣服的顺序。你是先穿袜子还是先穿裤子?你是先刷牙还是先洗脸?这个顺序能改变吗?
Let us do a little experiment. Imagine it is seven in the morning, and you need to get dressed for school. Please close your eyes and carefully recall in your mind the order in which you get dressed. Do you put on socks or pants first? Do you brush your teeth or wash your face first? Can this order be changed?
如果你仔细思考,你会发现穿衣服其实有一套“最佳顺序”。比如,你不可能先穿鞋子再穿袜子,也不可能先戴帽子再梳头发。这套顺序就是一套简单的算法——一套告诉你“如何穿好衣服去上学”的算法。虽然你可能从来没有用“算法”这个词来形容它,但事实上,你每天都在使用算法。
If you think carefully, you will find that getting dressed actually has a "best order." For example, you cannot put on shoes before socks, nor can you wear a hat before combing your hair. This set of order is a simple algorithm—an algorithm that tells you "how to get dressed for school." Although you may have never used the word "algorithm" to describe it, in fact, you use algorithms every day.
算法(Algorithm)这个词听起来很专业、很高深,但其实它的意思非常简单。算法就是解决问题的步骤说明书。就像食谱告诉你如何做一道菜,施工图纸告诉工人如何建造一座房子,算法就是告诉计算机(或者告诉我们自己)如何一步一步解决某个问题的详细步骤。
The word "algorithm" sounds very professional and profound, but its meaning is actually very simple. An algorithm is a step-by-step instruction manual for solving a problem. Just as a recipe tells you how to make a dish, construction drawings tell workers how to build a house, an algorithm is a detailed set of steps that tells a computer (or tells ourselves) how to solve a problem step by step.
为什么我们需要算法呢?因为世界上的问题太多了,有些问题非常简单,比如“1加1等于几”,我们的大脑可以瞬间给出答案。但还有很多问题非常复杂,比如“如何让快递在最短时间内送到你手里”、“如何在一秒钟内从千万本书中找到你要的那一本”……这些问题凭我们的大脑很难快速解决,这时候算法就派上用场了。一个好的算法可以帮助我们又快又准确地解决问题。
Why do we need algorithms? Because there are so many problems in the world. Some problems are very simple, like "what is 1 plus 1," and our brains can give the answer instantly. But there are many problems that are very complex, such as "how to deliver a package to you in the shortest time," "how to find the exact book you want among millions of books in one second"... These problems are difficult for our brains to solve quickly. This is when algorithms come in handy. A good algorithm can help us solve problems quickly and accurately.
让我们再来看一个生活中的例子。假设你有一个装满各种玩具的盒子,现在你要把这些玩具按照大小排列整齐,从最大的到最小的。你会怎么做?
Let us look at another example from life. Suppose you have a box full of various toys, and now you want to arrange these toys in order by size, from largest to smallest. What would you do?
你可能会这样做:首先,你在盒子里找到最大的那个玩具,把它拿出来放在一边;然后,你在剩下的玩具中找到最大的那个,再拿出来;重复这个过程,直到所有玩具都按顺序排好。这个方法听起来很简单对吧?恭喜你,你刚刚使用了一种叫做“选择排序”(Selection Sort)的经典算法!
You might do it this way: First, you find the largest toy in the box and take it out and put it aside; then, you find the largest among the remaining toys and take it out; repeat this process until all toys are arranged in order. This method sounds very simple, right? Congratulations, you just used a classic algorithm called "Selection Sort"!
算法的奇妙历史 / The Wonderful History of Algorithms
现在我们知道算法是什么了,那么算法是从什么时候开始有的呢?让我们坐上时间机器,回到几千年前去看看吧!
Now we know what an algorithm is, so when did algorithms begin? Let us get on a time machine and go back thousands of years to find out!
时间线图表
| 时期 | 文明 | 重要贡献 | |------|------|----------| | 公元前1800年 | 古巴比伦 | 泥板算法 | | 公元前300年 | 古希腊 | 欧几里得算法 | | 公元820年 | 阿拉伯 | “算法”一词的诞生 | | 公元1842年 | 英国 | 世界上第一个程序 | | 公元1936年 | 英国/美国 | 计算机科学的诞生 | | 公元1950年代至今 | 全球 | 算法爆炸式发展 |
Timeline Chart
| Period | Civilization | Important Contribution | |--------|--------------|------------------------| | 1800 BCE | Ancient Babylon | Clay tablet algorithms | | 300 BCE | Ancient Greece | Euclidean algorithm | | 820 CE | Arab world | Birth of the word "algorithm" | | 1842 CE | Britain | First computer program | | 1936 CE | Britain/USA | Birth of computer science | | 1950s to present | Global | Explosive development of algorithms |
古巴比伦人的智慧 / The Wisdom of the Babylonians
公元前1800年左右,在现在叫做伊拉克的地方,居住着古巴比伦人。他们非常聪明,发明了楔形文字,还建造了令人惊叹的空中花园。但是你可能不知道,他们也是世界上最早使用算法的人之一!
Around 1800 BCE, in what is now Iraq, the ancient Babylonians lived. They were very smart and invented cuneiform writing, and they also built the amazing Hanging Gardens. But you probably do not know that they were also among the first people in the world to use algorithms!
考古学家发现了一些刻在泥板上的古代“教科书”。这些泥板看起来就像我们现在的数学练习册,上面写着各种数学问题的解法。比如,有一块泥板展示了如何计算两个数相乘的结果,步骤写得清清楚楚:先做什么,再做什么,最后做什么。这其实就是古代的算法!
Archaeologists have discovered ancient "textbooks" carved on clay tablets. These clay tablets look like our modern math workbooks, with solutions to various math problems written clearly on them. For example, one tablet shows how to multiply two numbers, with the steps written clearly: first do this, then do that, finally do this. This is actually an ancient algorithm!
古巴比伦人用这些算法来解决实际问题,比如计算庄稼的收成、分配食物、建造房屋等。想象一下,几千年前的人就在使用算法来解决生活中的问题了,是不是很厉害?
The ancient Babylonians used these algorithms to solve practical problems, such as calculating crop yields, distributing food, and building houses. Imagine that people thousands of years ago were using algorithms to solve problems in life. Isn't that amazing?
欧几里得的伟大发现 / The Great Discovery of Euclid
让我们再往前跳,来到公元前300年左右的古希腊。那时候有一位非常伟大的数学家叫做欧几里得(Euclid),他写的一本书叫做《几何原本》(Elements),这本书在后来的两千多年里一直是数学教科书!
Let us jump forward again to ancient Greece around 300 BCE. At that time, there was a very great mathematician named Euclid, who wrote a book called "Elements," which remained a math textbook for more than two thousand years!
欧几里得发明了一个非常著名的算法,叫做“欧几里得算法”(Euclidean Algorithm),用来求两个整数的最大公约数(GCD - Greatest Common Divisor)。什么是最大公约数呢?比如说,12和8的最大公约数就是4,因为4是能够同时整除12和8的最大数字。
Euclid invented a very famous algorithm called the "Euclidean Algorithm," used to find the Greatest Common Divisor (GCD) of two integers. What is the greatest common divisor? For example, the greatest common divisor of 12 and 8 is 4, because 4 is the largest number that can divide both 12 and 8.
欧几里得算法是怎么回事呢?假设我们要找12和8的最大公约数。算法是这样的:用较大的数除以较小的数,看看余数是多少。12除以8等于1,余数是4。然后,用除数(8)除以余数(4),看看余数是多少。8除以4等于2,余数是0。当余数变成0的时候,最后一次的除数(4)就是最大公约数。
What is the Euclidean algorithm about? Suppose we want to find the greatest common divisor of 12 and 8. The algorithm works like this: Divide the larger number by the smaller number, and see what the remainder is. 12 divided by 8 equals 1, remainder 4. Then, divide the divisor (8) by the remainder (4), and see what the remainder is. 8 divided by 4 equals 2, remainder 0. When the remainder becomes 0, the last divisor (4) is the greatest common divisor.
这个算法看起来简单,但是它非常强大!即使过了两千多年,计算机科学家们仍然在使用这个算法,因为它非常高效,能够快速计算出最大公约数。有趣的是,现在我们使用的很多智能手机和电脑,在内部计算数学问题时,很可能就在使用基于欧几里得算法的思想!
This algorithm looks simple, but it is very powerful! Even after more than two thousand years, computer scientists are still using this algorithm because it is very efficient and can quickly calculate the greatest common divisor. Interestingly, many of our smartphones and computers today, when calculating math problems internally, are likely using ideas based on the Euclidean algorithm!
“算法”这个词的诞生 / The Birth of the Word "Algorithm"
时间跳到公元820年左右,地点是波斯的巴格达。那时候有一个非常著名的学者叫做花拉子密(Al-Khwarizmi)。花拉子密写了一本非常重要的数学书,在这本书里,他介绍了一种用阿拉伯数字进行计算的系统。
Let us jump forward to around 820 CE, in Baghdad, Persia. At that time, there was a very famous scholar named Al-Khwarizmi. Al-Khwarizmi wrote a very important math book in which he introduced a system for calculating using Arabic numerals.
花拉子密的名字“花拉子密”在拉丁语中逐渐变形,变成了“