gpt4 book ai didi

algorithm - 用于多个函数的大 O 表示法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:45:16 24 4
gpt4 key购买 nike

当一个人使用多个函数时,我有一个关于大 O 符号的问题。假设我想找出以下伪代码的时间复杂度:

heap sort array of size n
for i = 1 to n{
retrieve array[i]
change value of array[i]
}

我知道使用堆排序是 O(n log(n))。由于检索和更改数组中的数据的时间复杂度为 O(1),因此循环的复杂度为 O(n)。现在我的问题是:整个代码的复杂性是什么?它只是最大的时间复杂度吗? O(n log(n)) 在这种情况下?如果是这样,那么看起来像这样的函数的复杂性是什么:

for i = 1 to n{
// nothing fancy here
}
for y = 1 to n{
// nothing fancy here either
}

提前致谢。

最佳答案

for i = 1 to n{
// nothing fancy here
} //O(n)

for y = 1 to n{
// nothing fancy here
} //O(n)

所以加起来是 O(n) + O(n) = O(n)

关于algorithm - 用于多个函数的大 O 表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4934349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com