算法复杂度的表示法_用简单的英语算法:时间复杂度和Big-O表示法

算法复杂度的表示法by Michael Olorunnisola 通过Michael Olorunnisola 用简单的英语算法:时间复杂度和Big-O表示法 (Algorithms in plain English: time complexity and Big-O notation)Every good developer has time on their mind. They wa...
摘要由CSDN通过智能技术生成

算法复杂度的表示法

by Michael Olorunnisola

通过Michael Olorunnisola

用简单的英语算法:时间复杂度和Big-O表示法 (Algorithms in plain English: time complexity and Big-O notation)

Every good developer has time on their mind. They want to give their users more of it, so they can do all those things they enjoy. They do this by minimizing time complexity.

每个优秀的开发人员都有自己的时间。 他们想给用户更多的东西,以便他们可以做自己喜欢的所有事情。 他们通过最小化时间复杂度来做到这一点。

Before you can understand time complexity in programming, you have to understand where it’s most commonly applied: in the design of algorithms.

在您了解编程中的时间复杂性之前,您必须了解它最常应用的地方:在算法设计中。

那么,什么是算法? (So what’s an algorithm, anyway?)

Simply put, an algorithm is a series of contained steps, which you follow in order to achieve some goal, or to produce some output. Let’s take for example your grandma’s recipe for baking a cake. Wait, does that count as an algorithm? Sure it does!

简而言之,算法是一系列包含的步骤,为了实现某些目标或产生某些输出,必须遵循这些步骤。 让我们以您奶奶的烤蛋糕食谱为例。 等等,算作算法吗? 当然可以!

function BakeCake(flavor, icing){
"
 1. Heat Oven to 350 F
 2. Mix flour, baking powder, salt
 3. Beat butter and sugar until fluffy
 4. Add eggs.
 5. Mix in flour, baking powder, salt
 6. Add milk and " + flavor + "
 7. Mix further
 8. Put in pan
 9. Bake for 30 minutes
10." + if(icing === true) return 'add icing' + "
10. Stuff your face
"
}

BakeCake('vanilla', true) => deliciousness

Algorithms are useful in our examination of time complexity because they come in all shapes and sizes.

由于算法具有各种形状和大小,因此在我们检查时间复杂度方面很有用。

In the same way you can slice a pie a 100 different ways, you can solve a single problem with many different algorithms. Some solutions are just more efficient, taking less time and requiring less space than others.

以相同的方式,您可以用100种不同的方式对一个饼进行切片,可以使用许多种不同的算法来解决一个问题。 与其他解决方案相比,某些解决方案效率更高,所需时间更少,占用的空间更少。

So the main question is: how do we go about analyzing which solutions are most efficient?

因此,主要问题是:我们如何分析哪些解决方案最有效?

Math to the rescue! Time complexity analysis in programming is just an extremely simplified mathematical way of analyzing how long an algorithm with a given number of inputs (n) will take to complete it’s task. It’s usually defined using Big-O notation.

求救数学! 编程中的时间复杂度分析只是一种极其简化的数学方法,用于分析具有给定数量输入(n)的算法完成任务所需的时间。 通常使用Big-O表示法定义。

您问什么是大O符号? (What’s Big O notation, you ask?)

If you promise you won’t give up and stop reading, I will tell you.

如果您保证不会放弃并停止阅读,我会告诉您。

Big-O notation is a way of converting the overall steps of an algorithm into algebraic terms, then excluding lower order constants and coefficients that don’t have that big an impact on the overall complexity of the problem.

Big-O表示法是一种将算法的整体步骤转换为代数项,然后排除对问题的整体复杂性影响不大的低阶常量和系数的方法。

Mathematicians will probably cringe a bit at my “overall impact” assumption there, but for developers to save time, it’s easier to simplify things this way:

数学家可能会对我的“总体影响”假设有些畏缩,但是对于开发人员来说,为了节省时间,用这种方式简化事情会更容易:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值