如何计算函数的搜索复杂度

how to work out the search complexity of a function

本文关键字:函数 搜索 复杂度 计算 何计算      更新时间:2023-10-16

我有以下两个函数,我正在尝试比较时间复杂度 我的第一个函数是一个简单的 for 循环:

For (i=0;i<m;i++)
// do stuff

时间复杂度:

i=0 执行一次

i

i++ 执行 n 次

=2n+2 = 0(N(

我正在努力计算第二个函数的时间复杂度:

void search(string word)
{
Index index;
if (tree.AVL_Retrieve(word, index)) {
for (auto i : index.idList)
{

for (int j=0;j<m;j++)              
{
//do stuff
}
}   
}
}

我相信在 AVL 树中检索是 O(logN(,我的循环是 O(N(,然后我的内部循环是 O(M(,但作为一个整体,我将如何编写该搜索函数的时间复杂度。

注意:我们可以假设我的AVL树中有N个键

第一个 for 循环运行(m-1)次,因此具有时间复杂度 O(m(。

第二个函数运行一次AVL_Retrieve函数,并且针对 index.idList 的每个循环计数运行一次,给出复杂性O(log (number of nodes in tree))+O((count of index.idList)*m)