算法时间复杂度计算_算法时间复杂度简介

算法时间复杂度计算

In computer science, analysis of algorithms is a very crucial part. It is important to find the most efficient algorithm for solving a problem. It is possible to have many algorithms to solve a problem, but the challenge here is to choose the most efficient one.

在计算机科学中,算法分析是非常关键的部分。 找到解决问题的最有效算法很重要。 可能有很多算法可以解决问题,但是这里的挑战是选择最有效的算法。

Now the point is, how can we recognize the most efficient algorithm if we have a set of different algorithms? Here, the concept of space and time complexity of algorithms comes into existence. Space and time complexity acts as a measurement scale for algorithms. We compare the algorithms on the basis of their space (amount of memory) and time complexity (number of operations).

现在的关键是,如果我们有一组不同的算法,我们如何才能识别出最有效的算法? 在此,算法的时空复杂性概念应运而生。 时空复杂度是算法的度量尺度。 我们根据算法的空间(内存量)和时间复杂度(操作数)对算法进行比较。

The total amount of the computer's memory used by an algorithm when it is executed is the space complexity of that algorithm. We’ll not discuss space complexity in this article (to make this article a bit smaller).

算法执行时使用的计算机内存总量就是该算法的空间复杂度。 我们不会在本文中讨论空间复杂性(以使本文更小一些)。

时间复杂度 (Time Complexity)

So, the time complexity is the number of operations an algorithm performs to complete its task (considering that each operation takes the same amount of time). The algorithm that performs the task in the smallest number of operations is considered the most efficient one in terms of the time complexity. However, the space and time complexity are also affected by  factors such as your operating system and hardware, but we are not including them in this discussion.

因此,时间复杂度是算法完成其任务所执行的操作数(考虑到每个操作花费相同的时间)。 就时间复杂度而言,以最少数量的操作执行任务的算法被认为是效率最高的算法。 但是,空间和时间的复杂性也受诸如操作系统和硬件等因素的影响,但是我们不在此讨论中将它们包括在内。

Now to understand the time complexity, we will take an example in which we’ll compare two different algorithms which are used to solve a particular problem.

现在,要了解时间的复杂性,我们将举一个例子,比较两个用于解决特定问题的算法。

The problem is searching. We have to search for an element in an array (in this problem, we are going to assume that the array is sorted in  ascending order). To solve this problem we have two algorithms:

问题正在搜索。 我们必须在数组中搜索一个元素(在这个问题中,我们将假定数组以升序排序)。 为了解决这个问题,我们有两种算法:

1. Linear Search.

1. 线性搜索。

2. Binary Search.

2. 二进制搜索。

Let’s say the array contains ten elements, and we have to find the number ten in the array.

假设数组包含十个元素,我们必须在数组中找到数字十。

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const search_digit = 10;

Linear search algorithm will compare each element of the array to the search_digit. When it finds the search_digit in the array, it will return true.

线性搜索算法会将数组的每个元素与search_digit进行比较 当在数组中找到search_digit时,它将返回true

Now let’s count the number of operations it performs. Here, the answer is 10 (since it compares every element of the array). So,  Linear search uses ten operations to find the given element (these are the maximum number of operations for this array; in the case of Linear search, this is also known as the worst case of an algorithm).

现在,让我们计算一下它执行的操作数。 在这里,答案是10(因为它比较了数组的每个元素)。 因此,线性搜索使用十个运算来找到给定的元素(这些是该数组的最大运算数;在线性搜索的情况下,这也被称为算法的最坏情况 )。

In general, Linear search will take n number of operations in its worst case (where n is the size of the array).

通常,线性搜索在最坏的情况下将执行n次操作(其中n是数组的大小)。

Let’s examine the Binary search algorithm for this case.

让我们研究这种情况下的二进制搜索算法。

Binary search can be easily understood by this example:

通过此示例可以轻松理解二进制搜索:

Source: Learneroo

资料来源: Learneroo

If we try to apply this logic on our problem then, first we’ll compare search_digit with the middle element of the array, that is 5. Now since 5 is less than 10, then we will start looking for the search_digit in the array elements greater than 5, in the same way until we get the desired element 10.

如果我们尝试对问题应用此逻辑,则首先将search_digit与数组的中间元素(即5)进行比较。由于5小于10,因此我们将开始在数组元素中寻找search_digit大于5,直到获得所需元素10为止。

Now, try to count the number of operations binary search took to find the desired element. It took approximately four operations. Now, this was the worst case for binary search. This shows that there is a logarithmic relation between the number of operations performed and the total size of the array.

现在,尝试计算二进制搜索找到所需元素所需的操作数。 大约进行了四次手术。 现在,这是二进制搜索的最坏情况。 这表明执行的操作数与数组的总大小之间存在对数关系。

number of operations = log(10) = 4(approx) for base 2

运算数= 以2为底的 log(10)= 4(大约)

We can generalize this result for Binary search as:

对于二进制搜索,我们可以将结果概括为:

For an array of size n, the number of operations performed by the Binary Search is: log(n)

对于大小为n的数组,二进制搜索执行的操作数为: log(n)

大O符号 (The Big O Notation)

In the above statements, we saw that for an array of size n, linear search will perform n operations to complete the search. On the other hand, Binary search performed log(n) number of operations (both for their worst cases). We can represent this as a graph (x-axis: number of elements, y-axis: number of operations).

在上面的语句中,我们看到对于大小为n的数组,线性搜索将执行n个操作来完成搜索。 另一方面,二进制搜索执行log(n)个操作数(均为最坏情况)。 我们可以将其表示为图形( x轴 :元素数, y轴 :操作数)。

Source: Techtud

资料来源: Techtud

It is quite clear from the figure that the rate by which the complexity increases for Linear search is much faster than that for binary search.

从图中可以很清楚地看出,线性搜索的复杂度增加速度远快于二进制搜索的速度。

When we analyse an algorithm, we use a notation to represent its time complexity and that notation is Big O notation.

当我们分析算法时,我们使用一种表示法来表示其时间复杂度,并且该表示法是Big O表示法。

For Example: time complexity for Linear search can be represented as O(n) and O(log n) for Binary search (where, n and log(n) are the number of operations).

例如:线性搜索的时间复杂度可以表示为二进制搜索的O(n)O(log n) (其中nlog(n)是操作数)。

The Time complexity or Big O notations for some popular algorithms are listed below:

下面列出了一些流行算法的时间复杂度或Big O表示法:

  1. Binary Search: O(log n)

    二进制搜索:O(log n)
  2. Linear Search: O(n)

    线性搜索:O(n)
  3. Quick Sort: O(n * log n)

    快速排序:O(n * log n)
  4. Selection Sort: O(n * n)

    选择排序:O(n * n)
  5. Travelling salesperson : O(n!)

    旅行营业员:O(n!)

结论 (Conclusion)

I really appreciate your efforts if you are still reading this article. Now, you must be thinking - why is time complexity so important to understand?

如果您仍在阅读本文,我将非常感谢您的努力。 现在,您必须在思考-为什么时间复杂度如此重要才能理解?

We know that for a small number of elements (say 10), the difference between the number of operations performed by binary search and linear search is not so big. But in the real world, most of the time, we deal with  problems that have big chunks of data.

我们知道,对于少量元素(例如10),二进制搜索和线性搜索执行的操作数之间的差异并不大。 但是在现实世界中,大多数时候,我们要处理的数据量很大。

For example, if we have 4 billion elements to search for, then, in its worst case, linear search will take 4 billion operations to complete its task. Binary search will complete this task in just 32 operations. That’s a big difference. Now let’s assume that if one operation takes 1 ms for completion, then binary search will take only 32 ms whereas linear search will take 4 billion ms (that is approx. 46 days). That’s a significant difference.

例如,如果我们要搜索40亿个元素,那么在最坏的情况下,线性搜索将需要40亿次运算才能完成其任务。 二进制搜索仅需32次操作即可完成此任务。 那是很大的不同。 现在假设如果一个操作完成需要1毫秒,那么二进制搜索将仅花费32毫秒,而线性搜索将花费40亿毫秒(约46天)。 那是一个很大的差异。

This is the reason why studying time complexity becomes important when it comes to such a big amount of data.

这就是为什么在涉及大量数据时研究时间复杂性变得重要的原因。

资源资源 (Resources)

Grokking Algorithms- by Aditya Y Bhargava

Grokking算法-Aditya Y Bhargava着

Introduction to Big O notation and Time Complexity- by CS Dojo

大O符号和时间复杂性简介-CS Dojo

翻译自: https://www.freecodecamp.org/news/time-complexity-of-algorithms/

算法时间复杂度计算

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值