提高插入排序的运行时复杂性
Improving run time complexity of insert sort?
我编写了以下代码来尝试在 O(log n) 运行时中执行插入到排序向量中:
std::vector<int> to_insert_sort {2,3,5,6,1,1,3,2};
int to_insert {3};
std::sort(to_insert_sort.begin(), to_insert_sort.end()); //O(log n)
auto pos = std::upper_bound(to_insert_sort.begin(), to_insert_sort.end(), 5); //O(log n)
to_insert_sort.insert(pos, to_insert); //O(n)
1)我说这个操作是O(log n)是对的吗?
2) 是否有办法以更高的运行时效率执行此操作?
1)我说这个操作是O(log n)是对的吗?
std::sort
O(n * log n)
std::upper_bound
O(log n)
std::vector.insert
O(n)
2) 是否有办法在增加运行时间的情况下执行此操作 效率?
是的,您可以插入 O(log n),但您需要更改数据结构。例如,您可以使用 std::multiset<int>
而不是 std::vector<int>
。
std::multiset<int> m;
//m<={2,3,5,6,1,1,3,2}; //O(n * log n)
int to_insert {3};
m.insert(to_insert); //O(log n)
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 我在c++代码中生成了一个运行时#3异常
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 建议在运行时将带有类实例的列表从c++导入qml
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 如何在GTK程序运行时禁用屏幕保护程序/电源管理/屏幕消隐
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 读取文件时运行时的未知行为
- 这个递归函数有什么作用?运行时的复杂性是多少?
- 提高插入排序的运行时复杂性
- 此代码的运行时复杂性是多少
- 使得运行时复杂性更短(c++)
- std::list::p ush_back() 的运行时复杂性