本文翻译自:Big-O for Eight Year Olds? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
I'm asking more about what this means to my code. 我问的更多关于这对我的代码意味着什么。 I understand the concepts mathematically, I just have a hard time wrapping my head around what they mean conceptually. 我在数学上理解这些概念,我只是很难在概念上围绕它们的意思。 For example, if one were to perform an O(1) operation on a data structure, I understand that the number of operations it has to perform won't grow because there are more items. 例如,如果要对数据结构执行O(1)操作,我理解它必须执行的操作数量不会增加,因为有更多项目。 And an O(n) operation would mean that you would perform a set of operations on each element. 而O(n)操作意味着您将对每个元素执行一组操作。 Could somebody fill in the blanks here? 有人可以在这里填空吗?
- Like what exactly would an O(n^2) operation do? 就像O(n ^ 2)操作究竟会做什么一样?
- And what the heck does it mean if an operation is O(n log(n))? 如果一个操作是O(n log(n)),这意味着什么呢?
- And does somebody have to smoke crack to write an O(x!)? 有人必须抽烟才能写出O(x!)?
#1楼
参考:https://stackoom.com/question/RsT/岁儿童的大O-重复
#2楼
A lot of these are easy to demonstrate with something non-programming, like shuffling cards. 很多这些很容易用非编程的东西来展示,比如洗牌。
Sorting a deck of cards by going through the whole deck to find the ace of spades, then going through the whole deck to find the 2 of spades, and so on would be worst case n^2, if the deck was already sorted backwards. 通过穿过整个甲板找到黑桃王牌,然后通过整个甲板找到黑桃2,然后如果甲板已经向后排序,那么最坏情况n ^ 2将一副纸牌排序。 You looked at all 52 cards 52 times. 你看了所有52张牌52次。
In general the really bad algorithms aren't necessarily intentional, they're commonly a misuse of something else, like calling a method that is linear inside some other method that repeats over the same set linearly. 一般来说,真正糟糕的算法并不一定是故意的,它们通常是对其他东西的误用,比如在一些其他方法中调用线性方法,这种方法在线性上重复同一组。
#3楼
No, an O(n) algorithm does not mean it will perform an operation on each element. 不,O(n)算法并不意味着它将对每个元素执行操作。 Big-O notation gives you a way to talk about the "speed" of you algorithm independent of your actual machine. Big-O表示法为您提供了一种方法,可以独立于您的实际机器来讨论算法的“速度”。
O(n) means that the time your algorithm will take grows linearly as your input increase. O(n)表示算法采用的时间随着输入的增加而线性增长。 O(n^2) means that the time your algorithm takes grows as the square of your input. O(n ^ 2)表示算法占用的时间随着输入的平方而增长。 And so forth. 等等。
#4楼
One way of thinking about it is this: 一种思考方式是这样的:
O(N^2) means for every element, you're doing something with every other element, such as comparing them. O(N ^ 2)意味着对于每个元素,您正在对每个其他元素执行某些操作,例如比较它们。 Bubble sort is an example of this. 冒泡排序就是一个例子。
O(N log N) means for every element, you're doing something that only needs to look at log N of the elements. O(N log N)意味着对于每个元素,你正在做一些只需要查看元素的log N的东西。 This is usually because you know something about the elements that let you make an efficient choice. 这通常是因为您了解了可以让您做出有效选择的元素。 Most efficient sorts are an example of this, such as merge sort. 最有效的排序就是这样的一个例子,例如合并排序。
O(N!) means to do something for all possible permutations of the N elements. O(N!)表示为N个元素的所有可能排列做某事。 Traveling salesman is an example of this, where there are N! 旅行推销员就是一个例子,那里有N! ways to visit the nodes, and the brute force solution is to look at the total cost of every possible permutation to find the optimal one. 访问节点的方法,蛮力解决方案是查看每个可能的排列的总成本,以找到最佳的排列。
#5楼
You might find it useful to visualize it: 您可能会发现将其可视化很有用:
Also, on LogY/LogX scale the functions n 1/2 , n, n 2 all look like straight lines , while on LogY/X scale 2 n , e n , 10 n are straight lines and n! 此外,在LogY / LogX比例上,函数n 1/2 ,n,n 2都看起来像直线 ,而在LogY / X比例2 n ,e n ,10 n是直线和n! is linearithmic (looks like n log n ). 是线性的(看起来像n log n )。
#6楼
log(n) means logarithmic growth. log(n)表示对数增长。 An example would be divide and conquer algorithms. 一个例子是划分和征服算法。 If you have 1000 sorted numbers in an array ( ex. 3, 10, 34, 244, 1203 ... ) and want to search for a number in the list (find its position), you could start with checking the value of the number at index 500. If it is lower than what you seek, jump to 750. If it is higher than what you seek, jump to 250. Then you repeat the process until you find your value (and key). 如果数组中有1000个已排序的数字(例如3,10,34,244,1203 ......)并且想要在列表中搜索一个数字(找到它的位置),则可以从检查数值开始索引500处的数字。如果它低于你寻求的数字,跳转到750.如果它高于你寻求的数字,跳到250.然后你重复这个过程,直到你找到你的价值(和关键)。 Every time we jump half the search space, we can cull away testing many other values since we know the number 3004 can't be above number 5000 (remember, it is a sorted list). 每次我们跳过搜索空间的一半时,我们就可以剔除测试许多其他值,因为我们知道数字3004不能高于5000(记住,它是一个排序列表)。
n log(n) then means n * log(n). n log(n)则表示n * log(n)。