What is the cost/ complexity of insert in list at some location?

后端 未结 4 1091
慢半拍i
慢半拍i 2020-12-05 05:06

In Python, a list has list.insert(i, x) to \"Insert an item at a given position.\". In C++, there is a list as well. In C++, cost/complexity of inserting an el

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 05:39

    No, it is not the same complexity. According to Python's official Time Complexity page1, using list.insert always has O(n) (linear) complexity.

    Also, a Python list is not exactly the same as a C++ list. In fact, a Python list is more comparable to a C++ std::vector if anything.


    1Well, CPython's official page. I don't know about other implementations such as IronPython or Jython.

提交回复
热议问题