如何在此循环操作计数中找到此迭代的数量
How to find the number of iteration in this while loop - operation count
我对如何进行操作计数一段时间循环我感到困惑。我确实了解如何找到常规循环(从0到n(以及二进制搜索(log2n(的迭代数量,但是此代码利用了True和False的情况。迭代的数量将取决于"更多"是否为true,而"找到"是错误的。
最坏的情况是什么?找不到项目?在下面的代码中,注释的零件是该行的操作计数。
列表是n个节点的链接列表结构:
void FindItem(Node *list, Item item, Node *&loc, bool &found){
bool more = true; // 1
loc = list; found = false; // 2
while (more && !found) { // (number of iterations)
if (item < loc->info) // 2 * (number of iteration)
more = false; // (0 or 1)*number of iterations
else if (item == loc->info) // 2 * (number of iteration)
found=true; // (0 or 1)*number of iterations
else {
loc = loc->next; // (0 or 2) * (number of iteration)
more = (loc != NULL); // (0 or 2)*number of iterations
}
}
}
这看起来像学校练习或家庭作业问题。您需要在纸上回答的事实几乎证实了这一点。
因此,您要寻找的可能是"大O"的复杂性。在那种情况下,您正在查看一个简单的0 .. n循环,您声称自己知道,因为最多可以在整个列表上运行。
条件变量more
的名称及其自我的条件清楚地表明,代码不过是在排序列表上的linier搜索。
相关文章:
- 擦除while循环中迭代的元素
- 对于set上的循环-获取next元素迭代器
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 将多个 for 循环组合成单个迭代器
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- 使用一个 for 循环如何迭代和测试迭代器值?
- 循环挂起迭代的 std::擦除 on std::list
- 使用基于范围的循环迭代矢量时出现问题
- 整个循环花费的时间比其迭代总和要长
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- 转到基于范围的 for 循环中的下一个迭代器
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- C++ Code 在 for 循环期间不会累积变量中的总和,仅提供最终迭代值
- Visual Studio 2013 C 的迭代循环
- 矢量迭代循环引发错误
- C++:如何将数据从迭代循环输出到列中
- 迭代循环重复的次数比我预期的要多
- c++ 11线程在迭代循环时间
- 迭代循环与索引循环