什么是计算机算法,它们如何工作?

banner

Unless you’re into math or programming, the word “algorithm” might be Greek to you, but it’s one of the building blocks of everything you’re using to read this article. Here’s a quick explanation of what they are, and how they work.

除非您热衷于数学或编程,否则“算法”一词对您来说可能是希腊文,但这是您阅读本文所用的一切的组成部分之一。 这是它们的含义以及它们如何工作的简要说明。

Disclaimer: I’m not a math or computer science teacher, so not all of the terms I use are technical. That’s because I’m trying to explain everything in plain English for people aren’t quite comfortable with math. That being said, there is some math involved, and that’s unavoidable. Math geeks, feel free to correct or better explain in the comments, but please, keep it simple for the mathematically disinclined among us.

免责声明:我不是数学或计算机科学老师,所以我使用的所有术语并非全部都是技术性的。 那是因为我试图用通俗易懂的英语来解释一切,因为人们对数学不太满意。 话虽如此,其中涉及一些数学,这是不可避免的。 数学极客,随时可以在评论中进行更正或更好地解释,但请对我们中不喜欢数学的人保持简单。

Image by Ian Ruotsala

图片提供者: Ian Ruotsala

什么是算法? (What’s an Algorithm?)

The word ‘algorithm’ has an etymology similar to ‘algebra,’ except that this refers to the Arabic mathematician himself, al-Khwarizmi (just an interesting tidbit). An algorithm, for the non-programmers among us, is a set of instructions that take an input, A, and provide an output, B, that changes the data involved in some way. Algorithms have a wide variety of applications. In math, they can help calculate functions from points in a data set, among much more advanced things. Aside from their use in programming itself, they play major roles in things like file compression and data encryption.

“算法”一词的词源与“代数”相似,只不过是指阿拉伯数学家本人al-Khwarizmi(只是个有趣的花絮)。 对于我们中间的非程序员来说,一种算法是一组指令,这些指令采用输入A,并提供输出B,以某种方式更改所涉及的数据。 算法具有广泛的应用。 在数学上,它们可以帮助从数据集中的点计算函数,以及进行更高级的处理。 除了在编程本身中的用途外,它们还在文件压缩和数据加密等方面发挥重要作用。

基本指令集 (A Basic Set of Instructions)

Let’s say your friend is meeting you in a grocery store and you’re guiding him towards you. You say things like “come in through the right-side doors,” “pass the fish section on the left,” and “if you see the dairy, you passed me.” Algorithms work like that. We can use a flowchart to illustrate instructions based on criteria we know of ahead of time or find out during the process.

假设您的朋友在一家杂货店与您会面,而您正在引导他走向您。 您说诸如“从右侧门进来”,“经过左侧鱼区”和“如果看到乳制品,就越过我”之类的话。 算法就是这样工作的。 我们可以使用流程图来说明基于我们提前知道或在过程中发现的标准的指令。

icebreaking-routine

(image entitled “Icebreaking Routine” EDIT: courtesy of Trigger and Freewheel)

(图片标题为“ Icebreaking Routine ”编辑:由Trigger和Freewheel提供 )

From START, you would head down the path, and depending on what happens you follow the “flow” to an end result. Flowcharts are visual tools which can more understandably represent a set of instructions used by computers. Similarly, algorithms help do the same with more math-based models.

从START开始,您将沿着这条路前进,并根据发生的情况遵循“流程”直至最终结果。 流程图是可视工具,可以更理解地代表计算机使用的一组指令。 同样,算法可以帮助更多基于数学的模型实现同样的效果。

图表 (Graphs)

Let’s use a graph to illustrate the various ways we can give directions.

让我们用一个图形来说明我们给出指示的各种方法。

graph_drawn.gif

We can express this graph as a connection between all of its points. In order to reproduce this image, we can give a set of instructions to someone else.

我们可以将该图表示为其所有点之间的连接。 为了重现此图像,我们可以向其他人提供一组说明。

Method 1

方法1

We can represent this as a series of points, and the information would follow the standard form of graph = {(x1, y1), (x2, y2), …, (xn, yn)}.

我们可以将其表示为一系列点,信息将遵循图形的标准形式= {(x1,y1),(x2,y2),…,(xn,yn)}。

graph = {(0,0), (3,0), (3,3), (5,5), (7,10), (8,7), (9,4), (10,1)}

图表= {(0,0),(3,0),(3,3),(5,5),(7,10),(8,7),(9,4),(10,1) }

It’s pretty easy to plot each point, one after the other, and connect them to the previous point. However, imagine a graph with a thousand points or multiple segments all going every which way. That list would have a lot of data, right? And then having to connect each one, one at a time, can be a pain.

绘制每个点,一个接一个,然后将它们连接到上一个点非常容易。 但是,想象一下一个图形,它具有一千个点或多个段,每一个方向都在运行。 该列表将包含大量数据,对吗? 然后一次只能连接一个,可能会很痛苦。

Method 2

方法2

Another thing we can do is give a starting point, the slope of the line between it and the next point, and indicate where to expect the next point using the standard form of graph={(starting point}, [m1, x1, h1], …, [mn, xn, hn]}. Here, the variable ‘m’ represents the slope of the line, ‘x’ represents the direction to count in (whether x or y), and ‘h’ tells you how many to count in said direction. You can also remember to plot a point after each movement.

我们可以做的另一件事是给出一个起点,即起点与下一个点之间的直线的斜率,并使用graph = {(starting point},[m1,x1,h1 ],…,[mn,xn,hn]}。在这里,变量“ m”代表直线的斜率,“ x”代表计数的方向(无论是x还是y),“ h”告诉您如何您还可以记得在每次移动之后绘制一个点。

graph = {(0,0), [0,x,3], [0,y,3], [1,x,2], [2.5,x,2], [-3,x,1], [-3,x,1], [-3,x,1]}

图= {(0,0),[0,x,3],[0,y,3],[1,x,2],[2.5,x,2],[-3,x,1], [-3,x,1],[-3,x,1]}

You’ll end up with the same graph. You can see that the last three terms in this expression are the same, so we may be able to trim that down by just saying “repeat that three times” in some way. Let’s say that anytime you see the variable ‘R’ appear, it means to repeat the last thing. We can do this:

您将得到相同的图形。 您可以看到该表达式中的最后三个词是相同的,因此我们可以通过以某种方式说“重复三次”来将其减少。 假设当您看到变量“ R”出现时,这意味着重复最后一件事。 我们可以完成这个:

graph = {(0,0), [0,x,3], [0,y,3], [1,x,2], [2.5,x,2], [-3,x,1], [R=2]}

图= {(0,0),[0,x,3],[0,y,3],[1,x,2],[2.5,x,2],[-3,x,1], [R = 2]}

What if the individual points don’t really matter, and only the graph itself does? We can consolidate those last three sections like so:

如果各个点并不重要,而仅图形本身有关系怎么办? 我们可以像这样合并最后三个部分:

graph = {(0,0), [0,x,3], [0,y,3], [1,x,2], [2.5,x,2], [-3,x,3]}

图= {(0,0),[0,x,3],[0,y,3],[1,x,2],[2.5,x,2],[-3,x,3]}

It shortens things up a bit from where they were before.

它使事情比以前缩短了一点。

Method 3

方法3

Let’s try doing this another way.

让我们尝试另一种方式。

y=0, 0≤x≤3 x=0, 0≤y≤3 y=x, 3≤x≤5 y=2.5x-7.5, 5≤x≤7 y=-3x+29, 7≤x≤8 y=-3x+29, 8≤x≤9 y=-3x+29, 9≤x≤10

y = 0,0≤x≤3x = 0,0≤y≤3y = x,3≤x≤5y = 2.5x-7.5,5≤x≤7y = -3x + 29,7≤x≤ 8 y = -3x + 29,8≤x≤9y = -3x + 29,9≤x≤10

Here we have it in pure algebraic terms. Once again, if the points themselves don’t matter and only the graph does, we can consolidate the last three items.

在这里,我们用纯代数术语来表示它。 再说一次,如果这些点本身无关紧要,而只有图形才有关系,我们可以合并最后三个项目。

y=0, 0≤x≤3 x=0, 0≤y≤3 y=x, 3≤x≤5 y=2.5x-7.5, 5≤x≤7 y=-3x+29, 7≤x≤10

y = 0,0≤x≤3x = 0,0≤y≤3y = x,3≤x≤5y = 2.5x-7.5,5≤x≤7y = -3x + 29,7≤x≤ 10

Now, which method you pick depends on your abilities. Maybe you’re great with math and graphing, so you choose the last option. Maybe you’re good at navigating, so you choose the second option. In the realm of computers, however, you’re doing many different kinds of tasks and the computer’s ability doesn’t really change. Therefore, algorithms are optimized for the tasks they complete.

现在,您选择哪种方法取决于您的能力。 也许您最擅长数学和图形绘制,所以选择最后一个选项。 也许您擅长导航,所以选择第二个选项。 但是,在计算机领域,您正在执行许多不同类型的任务,并且计算机的功能并没有真正改变。 因此,算法针对其完成的任务进行了优化。

Another important point to note is that each method relies on a key. Each set of instructions is useless unless you know what to do with them. If you don’t know that you’re supposed to plot each point and connect the dots, the first set of points means nothing. Unless you know what each variable means in the second method, you won’t know how to apply them, much like the key to a cipher. That key is also an integral part of using algorithms, and often, that key is found in the community or via a “standard.”

要注意的另一个重要点是,每种方法都依赖于键。 除非您知道如何使用它们,否则每套指令都是无用的。 如果您不知道应该绘制每个点并连接点,则第一组点没有任何意义。 除非您知道每个变量在第二种方法中的含义,否则您将不知道如何应用它们,就像密码的密钥一样。 该密钥也是使用算法必不可少的部分,并且通常可以在社区中或通过“标准”找到该密钥。

文件压缩 (File Compression)

When you download a .zip file, you extract the contents so that you can use whatever is inside of it. Nowadays, most operating systems can dive into .zip files like they were normal folders, doing everything in the background. On my Windows 95 machine over a decade ago, I had to extract everything manually before I could see anything more than the filenames inside. That’s because what was stored on the disk as a .zip file was not in a usable form. Think of a pull-out couch. When you want to use it as a bed, you have to remove the cushions and unfold it, which takes up more space. When you don’t need it, or you want to transport it, you can fold it back up.

下载.zip文件时,您将提取内容,以便可以使用其中的任何内容。 如今,大多数操作系统都可以像普通文件夹一样潜入.zip文件,在后台执行所有操作。 十年前,在Windows 95机器上,我不得不手动提取所有内容,然后才能看到除文件名之外的任何内容。 这是因为作为.zip文件存储在磁盘上的内容不是可用格式。 想一想可拉出的沙发。 当您要将其用作床时,必须卸下垫子并将其展开,这会占用更多空间。 当您不需要它或要运输它时,可以将其折叠起来。

Compression algorithms are adjusted and optimized specifically for the types of files they are targeted to. Audio formats, for example, each use a different way to store data that, when decoded by the audio codec, will give a sound file similar to the original waveform. For more information on those difference, check out our previous article, What Are the Differences Between All Those Audio Formats? Lossless audio formats and .zip files have one thing in common: they both yield the original data in its exact form after the process of decompression. Lossy audio codecs use other means to save disk space, such as trimming frequencies that aren’t able to be heard by human ears and smoothing out the waveform in sections to get rid of some detail. In the end, while we may not be able to really hear the difference between an MP3 and a CD track, there’s definitely a deficit of information in the former.

专门针对压缩文件的类型对压缩算法进行了调整和优化。 例如,每种音频格式都使用不同的方式来存储数据,当通过音频编解码器解码时,这些数据将提供类似于原始波形的声音文件。 有关这些差异的更多信息,请查看我们以前的文章“所有那些音频格式之间有什么区别? 无损音频格式和.zip文件有一个共同点:在解压缩过程之后,它们都以原始格式产生原始数据。 有损的音频编解码器使用其他方式来节省磁盘空间,例如,修整人耳无法听到的频率,并平滑某些部分的波形以消除一些细节。 最后,尽管我们可能无法真正听到MP3和CD曲目之间的区别,但前者中肯定存在信息不足的问题。

数据加密 (Data Encryption)

enc-algorithms-(truecrypt)

Algorithms are also used when securing data or communication lines. Instead of storing data so that it uses less disk space, it’s stored in a manner that is undetectable by other programs. If someone steals your hard drive and starts to scan it, they can pick up data even when you delete files because the data itself is still there, even though the forwarding location to it is gone. When data is encrypted, whatever is stored doesn’t look like what it is. It usually looks random, as if fragmentation had built up over time. You can also store data and make it appear as another type of file. Image files and music files are good for this, as they can be quite large without drawing suspicion, for example. All of this is done by using mathematical algorithms, which take some kind of input and convert it into another, very specific type of output.  For more information on how encryption works, check out HTG Explains: What is Encryption and How Does It Work?

保护数据或通信线路时也使用算法。 它不是以存储较少磁盘空间的方式存储数据,而是以其他程序无法检测到的方式存储数据。 如果有人偷走了您的硬盘驱动器并开始对其进行扫描,那么即使您删除文件,他们也可以拾取数据,因为即使数据转发位置已经消失,数据本身仍然位于其中。 加密数据时,存储的内容看起来并不真实。 它通常看起来是随机的,好像碎片随着时间的推移而积累。 您还可以存储数据并使它显示为另一种文件类型。 图像文件和音乐文件对此非常有用,例如,它们可能很大而不会引起怀疑。 所有这些都是通过使用数学算法完成的,该算法采用某种输入并将其转换为另一种非常特定的输出类型。 有关加密如何工作的更多信息,请查看《 HTG解释:什么是加密以及它如何工作?



Algorithms are mathematical tools which provide a variety of uses in computer science. They work to provide a path between a start point and an end point in a consistent way, and provide the instructions to follow it. Know more than what we highlighted? Share your explanations in the comments!

算法是数学工具,可在计算机科学中提供多种用途。 它们以一致的方式提供起点和终点之间的路径,并提供遵循该路径的说明。 比我们强调的了解更多? 在评论中分享您的解释!

翻译自: https://www.howtogeek.com/howto/44052/htg-explains-what-are-computer-algorithms-and-how-do-they-work/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值