chapter_computational_complexity/time_complexity/ #18
Replies: 147 comments 219 replies
-
Beta Was this translation helpful? Give feedback.
-
本页中n^2的讲解中,以「冒泡排序」为例,外层循环n-1 次,内层循环···次,平均为n/2次,因此时间复杂度为 ··· 。在这里计算的是最差时间复杂度,是不是描述应该为计算内层冒泡排序次数的等差数列求和,而不是平均n/2再乘以外层(n-1)的描述,感觉容易造成歧义。 |
Beta Was this translation helpful? Give feedback.
-
最差 O 平均 θ 最优 Ω |
Beta Was this translation helpful? Give feedback.
-
大佬好,界面是不是加一个“返回顶部”的按钮会更方便些呀; |
Beta Was this translation helpful? Give feedback.
-
大佬你好,我是一个算法小白,在看公式时就有了困难,比如这个公式T(n)= 3 +2n,文中也没有具体写是怎么推导出来的,不仅如此,其他公式的推导好像也没有太详细。不知我是不是还该继续看下去。 |
Beta Was this translation helpful? Give feedback.
-
指数阶, 那个代码里面应该是 return expRecur(n - 2) + expRecur(n - 1) + 1吧; |
Beta Was this translation helpful? Give feedback.
-
哈喽,k大,worst_best_time_complexity.cpp 代码编译时出错 ➜ chapter_computational_complexity git:(master g++ worst_best_time_complexity.cpp
worst_best_time_complexity.cpp: In function ‘std::vector<int> randomNumbers(int)’:
worst_best_time_complexity.cpp:17:21: error: ‘chrono’ has not been declared
17 | unsigned seed = chrono::system_clock::now().time_since_epoch().count();
| ^~~~~~
worst_best_time_complexity.cpp:19:5: error: ‘shuffle’ was not declared in this scope
19 | shuffle(nums.begin(), nums.end(), default_random_engine(seed));
| ^~~~~~~ 原因是在
添加后编译通过,运行正常 ➜ chapter_computational_complexity git:(master g++ worst_best_time_complexity.cpp -o worst_best_time_complexity
➜ chapter_computational_complexity git:(master ls worst_best_time_complexity*
worst_best_time_complexity worst_best_time_complexity.cpp
|
Beta Was this translation helpful? Give feedback.
-
看不明白,这章好像只是知道了时间复杂度的表示方式。 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
K神,阶乘阶的操作总数应该不是等于最底层结点数量吧,是所有结点中的操作总数之和吗? |
Beta Was this translation helpful? Give feedback.
-
指数阶这段没看懂count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1。为什么最后等于2^n - 1 |
Beta Was this translation helpful? Give feedback.
-
能看到这本书真的太好了 |
Beta Was this translation helpful? Give feedback.
-
我不太清楚是不是我的系统问题,这两天一直在看,前几天似乎没问题。今天起的教程似乎所有显示数学公式的地方都出了问题?比如2.2.4中的一段变成了这样: 以下示例展示了使用上述技巧前、后的统计结果。 Edge 和 Chrome 都有这样的显示问题 |
Beta Was this translation helpful? Give feedback.
-
学习数据结构画图太重要了,作者的图画的太棒啦,可以知道是什么软件画的吗 |
Beta Was this translation helpful? Give feedback.
-
/* 指数阶(递归实现) */
function expRecur(n: number): number {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
} 这是在求什么,为什么最后要+1。没懂这里,谢谢。如果不要+1应该是求最后一层树结点数量,再+1就不知道是求什么了。请问是我理解错了吗?能不能分析一下这个函数的复杂度,谢谢大佬。已经理解了,是求全部节点数量,即操作次数,没毛病。谢谢大佬。太巧妙了。 总结为:n层的总节点数是n-1层总结点数*2 + 1,第1层总数为1,所以可以这样递归写。 看完gpt的答案我也懂了,感觉可以参考gpt的回答改进一下。 上述函数的时间复杂度为指数阶(Exponential Time),记作O(2^n)。 在函数中,每次递归调用expRecur(n - 1)会导致另外两次递归调用,因此函数的递归深度将以指数级增长。每次递归调用的时间复杂度是常数级的,但由于递归的次数是指数级的,因此整体时间复杂度也是指数级的。 简单来说,随着输入n的增加,函数的执行时间将指数增长,导致非常低效。因此,这是一个效率较低的实现,特别是在处理大型输入时。如果需要更高效的实现,可以考虑使用其他算法或优化技巧来减少递归的次数。 |
Beta Was this translation helpful? Give feedback.
-
请问第5点对数阶那里的图片(图2-12),虽然有logn+1层,但计算操作总数不应该是logn次吗?n=1时,不会执行while循环。有人可以解释一下吗?万分感谢!!! |
Beta Was this translation helpful? Give feedback.
-
请教一个问题: 为什么作者给出的两个函数第一个算上for循环那一步,第二个不算for循环那一步? def algorithm(n: int): def algorithm(n: int): |
Beta Was this translation helpful? Give feedback.
-
二叉树的高度:从跟节点到最远叶节点所经过的“边”的数量。而非节点的数量。 |
Beta Was this translation helpful? Give feedback.
-
初学者想请教个问题: 我们可以按照以下步骤计算运行时间: |
Beta Was this translation helpful? Give feedback.
-
继续学习继续加油!!! |
Beta Was this translation helpful? Give feedback.
-
😶🌫️ |
Beta Was this translation helpful? Give feedback.
-
okkk |
Beta Was this translation helpful? Give feedback.
-
day2 |
Beta Was this translation helpful? Give feedback.
-
2025.3.11 |
Beta Was this translation helpful? Give feedback.
-
打卡 |
Beta Was this translation helpful? Give feedback.
-
如果有人回复我消息,我该在哪里看到消息提醒?还是说我要回到这个章节翻我之前的评论我才能看见吗?感谢解答! |
Beta Was this translation helpful? Give feedback.
-
/* 阶乘阶(递归实现) */ |
Beta Was this translation helpful? Give feedback.
-
“时间复杂度能够有效评估算法效率。例如,算法 B 的运行时间呈线性增长,在 n>1时比算法 A 更慢,在 |
Beta Was this translation helpful? Give feedback.
-
真诚提问,想请问下大家这本书看了适合新人吗,怎么样。我看了两天感觉很多概念很晦涩,递归,分治,各种排序。我是一个转码纯小白,刚学完python,不知道是不是有适合新人的数据结构与算法推荐。谢谢! |
Beta Was this translation helpful? Give feedback.
-
打卡 2025.6.9 2.常见类型的时间复杂度 与指数阶相反,对数阶反映了“每轮缩减到一半”的情况。 3.最佳、最差、平均时间复杂度 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
chapter_computational_complexity/time_complexity/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_computational_complexity/time_complexity/
Beta Was this translation helpful? Give feedback.
All reactions