递归函数的复杂性 - 时间和空间

发布于 2024-10-05 02:00:08 字数 231 浏览 0 评论 0原文

我有兴趣知道如何计算递归函数的时间和空间复杂度,如排列、斐波那契(描述这里

一般来说,我们可以在很多地方进行递归,而不仅仅是排列或递归,所以我正在寻找通常遵循的方法来计算 tmie ans 空间复杂度

谢谢

I was interested in knowing how to calculate the time and space complexity of recursive functions like permutation, fibonacci(described here)

In general we can have recursion at many places than just at permutaion or recursion, so I am looking for approach generally followed to calculate tmie ans space complexity

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春庭雪 2024-10-12 02:00:08

时间复杂度和空间复杂度是表征算法性能的两个因素。如今,由于空间相对廉价,人们最关心的是时间复杂度,而时间复杂度大多用递归关系来表达。

考虑二分搜索算法(搜索数组中的元素):每次选择中间元素(mid)并将其与要搜索的元素(x)进行比较。如果(mid > x),则搜索下层子数组,否则搜索上层子数组。如果数组中有n个元素,设T(n)表示算法的时间复杂度,则
T(n) = T(n/2) + c,其中 c 是常数。在给定的边界条件下,我们可以求解 T(n),在这种情况下,它将是 T(n) = log(n),其中 T(1) = 1。

Time complexity and space complexity are the two things that characterize the performance of an algorithm. Nowadays, as space is relatively inexpensive, people bother mostly about time complexity, and time complexity is mostly expressed in terms of a recurrence relation.

Consider binary search algorithm (Search for an element in an array): Each time middle element(mid) is selected and compared with the element(x) to be searched. If (mid > x), search the lower sub-array, otherwise search the upper sub-array. If there are n elements in an array, and let T(n) represents the time complexity of the algorithm, then
T(n) = T(n/2) + c, where c is a constant. With given boundary conditions, we can solve for T(n), in this case, it would be T(n) = log(n), with T(1) = 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文