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

标签 c++ big-o

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

For (i=0;i<m;i++)
// do stuff
时间复杂度:

i=0 is executed once

i< n is executed n+1 times

i++ is executed n times

= 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)的复杂性

关于c++ - 如何计算函数的搜索复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61936228/

相关文章:

c++ - 通过 string::insert 插入 char 时出错

c++ - 关于C++默认值的一些问题

c++ - 构造函数在哪里获取/设置默认分配器?

algorithm - 具有嵌套 if 语句和 for 循环的算法的时间复杂度

javascript - 在循环中使用 indexOf 是个坏主意吗?

c++ - 在 Qt 中制作所见即所得

performance - 数组中的随机整数。找出连续子集的最大和

c++ - 处理输入 : growing an array, 或计数输入、分配、然后读取的速度

algorithm - 时间复杂度 - O(n^2) 到 O(n log n) 搜索

c++ - 有没有办法提示用户使用什么数据类型作为模板 C++