C++ 提高插入排序的运行时复杂性?
我编写了以下代码,试图在O(logn)运行时对排序向量执行插入操作:C++ 提高插入排序的运行时复杂性?,c++,runtime,time-complexity,C++,Runtime,Time Complexity,我编写了以下代码,试图在O(logn)运行时对排序向量执行插入操作: 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.e
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)
std::向量插入排序{2,3,5,6,1,1,3,2};
int到_插入{3};
std::sort(to_insert_sort.begin(),to_insert_sort.end())//O(对数n)
auto pos=std::上限(到插入排序.begin(),到插入排序.end(),5)//O(对数n)
插入排序插入(位置,插入)//O(n)
1) 我说这个操作是O(logn)对吗
2) 有没有提高运行时效率的方法来执行此操作
1) 我说这个操作是O(logn)对吗
std::sort
O(n*logn)
std::上限
O(日志n)
std::vector.insert
O(n)
2) 是否有方法在增加运行时间的情况下执行此操作
效率
是的,您可以插入O(日志n),但需要更改数据结构。例如,您可以使用std::multiset
而不是std::vector
std::multiset m;
//m
1) 我说这个操作是O(logn)对吗
std::sort
O(n*logn)
std::上限
O(日志n)
std::vector.insert
O(n)
2) 是否有方法在增加运行时间的情况下执行此操作
效率
是的,您可以插入O(日志n),但需要更改数据结构。例如,您可以使用std::multiset
而不是std::vector
std::multiset m;
//mt排序的复杂性不是O(logn)
,而是O(n*logn)
。看见所以我猜操作是O(n*logn)
我想这是作业吗?std::sort
的复杂性不是O(logn)
,而是O(n*logn)
。看见所以我猜操作是O(n*logn)
我想这是家庭作业吗?
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)