latex 插图解释_大O符号-只需插图和视频即可解释

latex 插图解释

Big O notation is used to communicate how fast an algorithm is. This can be important when evaluating other people’s algorithms, and when evaluating your own! In this article, I’ll explain what Big O notation is and give you a list of the most common running times for algorithms using it.

大O表示法用于传达算法的速度。 这在评估别人的算法以及评估自己的算法时可能很重要! 在本文中,我将解释Big O表示法是什么,并为您提供使用该算法的最常见运行时间的列表。

算法运行时间以不同的速度增长 (Algorithm running times grow at different rates)

My son Judah has a lot of toys. In fact, he has acquired a billion toys! You’d be surprised how quickly a kid can get that many toys if he’s the first grandchild on both sides of the family. ??

我的儿子犹大有很多玩具。 实际上,他已经获得了十亿个玩具! 如果一个孩子是家庭双方的第一个孙子,那么一个孩子这么快就能得到这么多玩具,您会感到惊讶。 ??

Anyway, Judah has a problem. When his friends visit and want to play with a specific toy, it can take FOREVER to find the toy. So he wants to create a search algorithm to help him find a specific toy as quick as possible. He is trying to decide between two different search algorithms: simple search and binary search. (Don’t worry if you are not familiar with these algorithms.)

无论如何,犹大有问题。 当他的朋友拜访并想玩特定的玩具时,可能需要永远找到玩具。 因此,他想创建一个搜索算法来帮助他尽快找到特定的玩具。 他正在尝试在两种不同的搜索算法之间做出选择:简单搜索和二进制搜索。 (如果您不熟悉这些算法,请不要担心。)

The algorithm chosen needs to be both fast and correct. On one hand, binary search is faster. And Judah often only has about 30 seconds before his friend gets bored looking for a toy. On the other hand, a simple search algorithm is easier to write, and there is less chance of bugs being introduced. It sure would be embarrassing if his friend found bugs in his code! To be extra careful, Judah decides to time both algorithms with a list of 100 toys.

选择的算法必须既快速又正确。 一方面,二进制搜索更快。 犹大常常只有大约3 0秒 ,他的朋友就会无聊地寻找玩具。 另一方面,简单的搜索算法更易于编写,而且引入错误的机会也更少。 如果他的朋友在他的代码中发现错误,那肯定会很尴尬! 为格外小心,犹大决定为这两种算法计时,并列出100个玩具。

Let’s assume it takes 1 millisecond to check one toy. With simple search, Judah has to check 100 toys, so the search takes 100 ms to run. On the other hand, he only has to check 7 toys with binary search (log2 100 is roughly 7, don’t worry if this math is confusing since it isn’t the main point of this article), so that search takes 7 ms to run. But really, the list will have a billion toys. If it does, how long will simple search take? How long will binary search take?

假设检查一个玩具需要1毫秒。 通过简单的搜索,犹大必须检查100个玩具,因此搜索需要100毫秒才能运行。 另一方面,他只需要通过二进制搜索来检查7个玩具(log2 100大约是7,所以不用担心这个数学是否令人困惑,因为它不是本文的重点),因此搜索需要7毫秒跑步。 但实际上,清单上将有十亿个玩具。 如果可以,简单搜索将花费多长时间? 二进制搜索需要多长时间?

简单搜索与二进制搜索的运行时间,列出了100个元素 (Running time for simple search vs. binary search, with a list of 100 elements)

Judah runs binary search with 1 billion toys, and it takes 30 ms (log2 1,000,000,000 is roughly 30). “32 ms!” he thinks. “Binary search is about 15 times faster than simple search, because simple search took 100 ms with 100 elements, and binary search took 7 ms. So simple search will take 30 × 15 = 450 ms, right? Way under the 30 seconds it takes for my friend to get bored.” Judah decides to go with simple search. Is that the right choice?

犹大使用10亿个玩具进行二进制搜索,耗时30毫秒(log2 1,000,000,000大约为30)。 “ 32毫秒!” 他想。 “二进制搜索比简单搜索快15倍,因为简单搜索用100个元素需要100毫秒,而二进制搜索则需要7毫秒。 这样简单的搜索将花费30×15 = 450 ms,对吗? 不到30秒,我的朋友就变得无聊了。” 犹大决定进行简单的搜索。 那是正确的选择吗?

No. Turns out, Judah was wrong and lost a friend for life. ? The run time for simple search with 1 billion items will be 1 billion ms, which is 11 days! The problem is, the run times for binary search and simple search don’t grow at the same rate.

没有。事实证明,犹大错了,终生失去了一个朋友。 ? 具有10亿个项目的简单搜索的运行时间将为10亿毫秒,即11天! 问题是,二进制搜索和简单搜索的运行时间不会以相同的速度增长。

Run times grow at very different speeds! As the number of items increases, binary search takes a little more time to run, but simple search takes a lot more time to run. So as the list of numbers gets bigger, binary search suddenly becomes a lot faster than simple search.

运行时间以不同的速度增长! 随着项目数量的增加,二进制搜索需要多一点时间来运行,但简单的搜索需要更多时间来运行。 因此,作为号码列表变大,二进制搜索突然变得很多比简单的搜索速度更快。

So Judah was wrong about binary search always being 15 times faster than simple search. If there are 1 billion toys, it’s more like 33 million times faster.

因此,犹大错了,因为二进制搜索总是比简单搜索快15倍。 如果有10亿个玩具,则速度要快3300万倍。

It is very important to know how the running time increases as the list size increases. That’s where Big O notation comes in.

了解运行时间如何随着列表大小的增加而增加非常重要。 那就是Big O表示法出现的地方。

Big O notation tells you how fast an algorithm is. For example, suppose you have a list of size n. Simple search needs to check each element, so it will take n operations. The run time in Big O notation is O(n).

大O表示法告诉您算法有多快。 例如,假设您有一个大小为n的列表。 简单搜索需要检查每个元素,因此将需要n次操作。 Big O表示法的运行时间为O( n )。

Where are the seconds? There are none — Big O doesn’t tell you the speed in seconds. Big O notation lets you compare the number of operations. It tells you how fast the algorithm grows.

秒在哪里? 没有-大O不会以秒为单位告诉您速度。 大O表示法使您可以比较操作数。 它告诉您算法的增长速度。

Let’s do another example. Binary search needs log n operations to check a list of size n. What’s the running time in Big O notation? It’s O(log n). In general, Big O notation is written as follows.

让我们再举一个例子。 二进制搜索需要log n次操作才能检查大小为n的列表。 Big O符号的运行时间是多少? 是O(log n )。 通常,Big O表示法如下所示。

This tells you the number of operations an algorithm will make. It’s called Big O notation because you put a “big O” in front of the number of operations.

这告诉您算法将执行的操作数。 之所以称为Big O表示法,是因为您在操作数前面放置了一个“ big O”。

大O确定最坏的运行时间 (Big O establishes a worst-case run time)

Suppose you’re using simple search to look for a user in your user database. You know that simple search takes O(n) time to run, which means in the worst case, you’re algorithm will have to look through every user in the database. In this case, you’re looking for a user with the name ‘aardvark213’. This is the first user in the list. So your algorithm didn’t have to look at every entry — it found it on the first try. Did the algorithm take O(n) time? Or did it take O(1) time because it found the person on the first try?

假设您正在使用简单搜索在用户数据库中查找用户。 您知道简单搜索需要O( n )时间来运行,这意味着在最坏的情况下,您的算法将必须遍历数据库中的每个用户。 在这种情况下,您正在寻找名称为“ aardvark213”的用户。 这是列表中的第一个用户。 因此,您的算法不必查看每个条目-它是在第一次尝试时就发现的。 该算法是否花费O( n )时间? 还是花了O(1)时间,因为它在第一次尝试中找到了那个人?

Simple search still takes O(n) time. In this case, the algorithm found what it was looking for instantly. That’s the best-case scenario. But Big O notation is about the worst-case scenario. So you can say that, in the worst case, the algorithm will have to look through every user in the database once. That’s O(n) time. It’s a reassurance — you know that simple search will never be slower than O(n) time.

简单搜索仍然需要O( n )时间。 在这种情况下,算法会立即找到其要查找的内容。 那是最好的情况。 但是Big O符号是关于最坏情况的。 因此,您可以说,在最坏的情况下 ,该算法将不得不遍历数据库中的每个用户一次。 那是O( n )时间。 可以放心-简单的搜索永远不会比O( n )时间慢。

一些常见的Big O运行时间 (Some common Big O run times)

Here are five Big O run times that you’ll encounter a lot, sorted from fastest to slowest:

这是您会遇到的五个大O运行时间,从最快到最慢排序:

  • O(log n), also known as log time. Example: Binary search.

    O(log n ),也称为对数时间。 示例:二进制搜索。

  • O(n), also known as linear time. Example: Simple search.

    O( n ),也称为线性时间 。 示例:简单搜索。

  • O(n * log n). Example: A fast sorting algorithm, like quicksort.

    O( n * log n )。 示例:一种快速排序算法,例如quicksort。

  • O(n2). Example: A slow sorting algorithm, like selection sort.

    O( n 2)。 示例:慢速排序算法,例如选择排序。

  • O(n!). Example: A really slow algorithm, like the traveling salesperson.

    O( n !) 示例:一个非常慢的算法,例如旅行营业员。

可视化不同的大O运行时间 (Visualizing different Big O run times)

Suppose you’re drawing a grid of 16 boxes, and you can choose from 5 different algorithms to do so. If you use the first algorithm, it will take you O(log n) time to draw the grid. You can do 10 operations per second. With O(log n) time, it will take you 4 operations to draw a grid of 16 boxes (log 16 base 2 is 4). So it will take you 0.4 seconds to draw the grid. What if you have to draw 1,024 boxes? It will take you log 1,024 = 10 operations, or 1 second to draw a grid of 1,024 boxes. These numbers are using the first algorithm.

假设您要绘制一个由16个框组成的网格,并且可以从5种不同的算法中进行选择。 如果使用第一种算法,则将花费O(log n )时间来绘制网格。 您每秒可以执行10次操作。 使用O(log n )时间,您将需要4次操作才能绘制一个由16个框组成的网格(log 16以2为底)。 因此,绘制网格将需要0.4秒。 如果您必须画1,024箱怎么办? 您将需要记录1,024 = 10个操作,或1秒才能绘制一个1,024个框的网格。 这些数字使用第一种算法。

The second algorithm is slower: it takes O(n) time. It will take 16 operations to draw 16 boxes, and it will take 1,024 operations to draw 1,024 boxes. How much time is that in seconds?

第二种算法较慢:需要O( n )时间。 绘制16个盒子需要16个操作,绘制1,024个盒子将需要1,024个操作。 以秒为单位的时间是多少?

Here’s how long it would take to draw a grid for the rest of the algorithms, from fastest to slowest:

这是为其余算法(从最快到最慢)绘制网格所需的时间:

There are other run times, too, but these are the five most common.

也有其他运行时间,但这是最常见的五个时间。

This is a simplification. In reality you can’t convert from a Big O run time to a number of operations this neatly, but this is good estimation.

这是一个简化。 实际上,您不能整齐地将Big O运行时转换为许多操作,但这是一个很好的估计。

结论 (Conclusion)

Here are the main takeaways:

这是主要的要点:

  • Algorithm speed isn’t measured in seconds, but in growth of the number of operations.

    算法速度不是以秒为单位,而是以操作数量的增长来衡量。
  • Instead, we talk about how quickly the run time of an algorithm increases as the size of the input increases.

    相反,我们谈论的是算法的运行时间随着输入大小的增加而增加的速度。
  • Run time of algorithms is expressed in Big O notation.

    算法的运行时间以Big O表示法表示。
  • O(log n) is faster than O(n), but it gets a lot faster as the list of items you’re searching grows.

    O(log n )比O( n )快,但是随着您要搜索的项目列表的增加,它变得更快。

And here is a video that covers a lot of what is in this article and more.

这是一个视频,涵盖了本文以及更多内容。

I hope this article brought you more clarity about Big O notation. This article is based on a lesson in my video course from Manning Publications called Algorithms in Motion. The course is based on the amazing book Grokking Algorithms by Adit Bhargava. He’s the one who drew all the fun illustrations in this article.

我希望本文能使您对Big O符号有更多的了解。 本文基于我的视频课程中曼宁出版物上的一门课程,该课程称为“ 运动中的算法” 。 该课程基于Adit Bhargava 撰写令人惊讶的Grokking Algorithms》一书。 他是绘制本文中所有有趣插图的人。

If you learn best through books, get the book! If you learn best through videos, consider buying my course. You can get 39% off my course by using the code ‘39carnes’.

如果您通过书本学得最好,请获取书本 ! 如果您通过视频学习得最好,请考虑购买我的课程 。 使用代码“ 39carnes ”,您可以从我的课程中获得39%的折扣。

翻译自: https://www.freecodecamp.org/news/big-o-notation-simply-explained-with-illustrations-and-video-87d5a71c0174/

latex 插图解释

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值