算法时间复杂度计算_简短的算法时间复杂度介绍

算法时间复杂度计算

Just like writing your very first for loop, understanding time complexity is an integral milestone to learning how to write efficient complex programs. Think of it as having a superpower that allows you to know exactly what type of program might be the most efficient in a particular situation — before even running a single line of code.

就像编写您的第一个for循环一样,了解时间复杂度是学习如何编写有效的复杂程序的重要里程碑。 可以认为它具有超强功能,可以使您确切地知道哪种类型的程序在特定情况下可能是最高效的,甚至可以只运行一行代码。

The fundamental concepts of complexity analysis are well worth studying. You’ll be able to better understand how the code you’re writing will interact with the program’s input, and as a result, you’ll spend a lot less wasted time writing slow and problematic code.

复杂性分析的基本概念非常值得研究。 您将能够更好地了解所编写的代码将如何与程序的输入进行交互,因此,您将花费更少的时间来编写缓慢而有问题的代码。

It won’t take long to go over all you need to know in order to start writing more efficient programs — in fact, we can do it in about fifteen minutes. You can go grab a coffee right now (or tea, if that’s your thing) and I’ll take you through it before your coffee break is over. Go ahead, I’ll wait.

为了开始编写更高效的程序,花很长时间可以遍历所有您需要了解的知识-实际上,我们可以在大约十五分钟内完成。 您现在可以去喝杯咖啡(或茶,如果那是您的事),我会在您的咖啡休息时间结束之前帮您煮一杯。 来吧,我等。

All set? Let’s do it!

可以了,好了? 我们开始做吧!

无论如何,“时间复杂度”是什么? (What is “time complexity” anyway?)

The time complexity of an algorithm is an approximation of how long that algorithm will take to process some input. It describes the efficiency of the algorithm by the magnitude of its operations. This is different than the number of times an operation repeats. I’ll expand on that later. Generally, the fewer operations the algorithm has, the faster it will be.

算法的时间复杂度是该算法处理某些输入将花费多长时间的近似值 。 它通过运算的大小来描述算法的效率。 这与操作重复的次数不同。 我将在稍后进行扩展。 通常,算法执行的运算越少,运算速度就越快。

We write about time complexity using Big O notation, which looks something like O(n). There’s rather a lot of math involved in its formal definition, but informally we can say that Big O notation gives us our algorithm’s approximate run time in the worst case, or in other words, its upper bound. It is inherently relative and comparative.

我们使用Big O表示法来编写时间复杂度,看起来像O ( n )。 它的正式定义涉及很多数学运算,但非正式地,我们可以说Big O符号为我们提供了算法在最坏情况下的近似运行时间,或者换句话说,它的上限。 它本质上是相对的和比较的。

We’re describing the algorithm’s efficiency relative to the increasing size of its input data, n. If the input is a string, then n is the length of the string. If it’s a list of integers, n is the length of the list.

我们正在描述算法相对于输入数据n的大小增加的效率。 如果输入是字符串,则n是字符串的长度。 如果它是整数列表,则n是列表的长度。

It’s easiest to picture what Big O notation represents with a graph:

用图形描绘Big O表示法最简单:

Here are the main important points to remember as you read the rest of this article:

阅读本文的其余部分时,请记住以下主要要点:

  • Time complexity is an approximation

    时间复杂度是一个近似值
  • An algorithm’s time complexity approximates its worst case run time

    算法的时间复杂度接近其最坏情况的运行时间

确定时间复杂度 (Determining time complexity)

There are different classes of complexity that we can use to quickly understand an algorithm. I’ll illustrate some of these classes using nested loops and other examples.

我们可以使用不同类别的复杂性来快速了解算法。 我将使用嵌套循环和其他示例来说明其中一些类。

多项式时间复杂度 (Polynomial time complexity)

A polynomial, from the Greek poly meaning “many,” and Latin nomen meaning “name,” describes an expression comprised of constant variables, and addition, multiplication, and exponentiation to a non-negative integer power. That’s a super math-y way to say that it contains variables usually denoted by letters, and symbols that look like these:

多项式 ,由希腊语poly表示“许多”,而拉丁语nomen表示“ name”,描述了一个表达式,该表达式包含常量变量以及加,乘和乘幂到非负整数幂。 这是一种超级数学的说法,它包含通常用字母表示的变量和如下所示的符号:

The below classes describe polynomial algorithms. Some have food examples.

以下类描述多项式算法。 有些有食物的例子。

不变 (Constant)

A constant time algorithm doesn’t change its running time in response to the input data. No matter the size of the data it receives, the algorithm takes the same amount of time to run. We denote this as a time complexity of O(1).

恒定时间算法不会响应输入数据而更改其运行时间。 无论接收到的数据大小如何,该算法都会花费相同的时间来运行。 我们将其表示为O (1)的时间复杂度。

Here’s one example of a constant algorithm that takes the first item in a slice.

这是一个常量算法的示例,该算法采用切片中的第一项。

func takeCupcake(cupcakes []int) int {
	return cupcakes[0]
}

With this contant-time algorithm, no matter how many cupcakes are on offer, you just get the first one. Oh well. Flavours are overrated anyway.

使用这种竞争时间算法,无论提供多少杯形蛋糕,您都只会得到第一个。 那好吧。 无论如何,口味被高估了。

线性的 (Linear)

The running duration of a linear algorithm is constant. It will process the input in n number of operations. This is often the best possible (most efficient) case for time complexity where all the data must be examined.

线性算法的运行持续时间是恒定的。 它将以n个操作处理输入。 对于时间复杂度,这通常是最好的(最有效的)情况,其中必须检查所有数据。

Here’s an example of code with time complexity of O(n):

这是时间复杂度为O ( n )的代码示例:

func eatChips(bowlOfChips int) {
	for chip := 0; chip <= bowlOfChips; chip++ {
		// dip chip
	}
}

Here’s another example of code with time complexity of O(n):

这是时间复杂度为O ( n )的另一个代码示例:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值