c++ - 列表类型的binary_search函数的时间复杂度是多少

标签 c++ sorting search time-complexity lower-bound

是 O( n ) 还是 O( logn ) ?

  list< int > myList = { 2, 6, 12, 13, 15, 18, 20};    
    cout << binary_search(myList.begin(), myList.end(), 20) ;

最佳答案

Complexity

The number of comparisons performed is logarithmic in the distance between first and last (At most log2(last - first) + O(1) comparisons). However, for non-LegacyRandomAccessIterators, number of iterator increments is linear.

(c) cppreference

std::list 迭代器不是随机访问迭代器(它们是前向迭代器),因此复杂度为 O(n)

关于c++ - 列表类型的binary_search函数的时间复杂度是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61286953/

相关文章:

c++ - 有没有办法从 Xcode 调试器的调用堆栈中删除内联函数?

arrays - 如何在 Swift 中对 NSDate 进行排序

python-3.x - 根据绘图颜色对相似度矩阵进行排序

javascript - 如何在 JavaScript 中搜索其他不区分大小写的对象?与 _.where() 一样,仅不区分大小写

c++ - 使用 C++ Hook QT 应用程序以捕获应用程序的文本名称

c++ - 将多个变量从一个类中的函数传递到另一个类中的函数

c++ - 是否可以使用 begin() 和 end() 创建 std::array?

iOS Swift 二维数组排序

python - 在列表列表中搜索字符串

ruby-on-rails - Solr(太阳黑子)在非关键字搜索上的查询时间提升