20

I have a sorted list L and I have a binary search for determining where in the list to insert an element such that the resulting list will still be in order.

However L.insert(index,object) needs O(N) time complexity.

Is there another data structure for L that will serve the same purpose, but allows for a faster insertion?

9
  • Binary search tree? Looks like Python doesn't have one built in, but there's probably a package somewhere for one. Commented Jun 5, 2015 at 2:02
  • Yeah Binary Search Tree is O(1) insertion. Commented Jun 5, 2015 at 2:03
  • 1
    @JamesMills Don't you mean O(log n)? Commented Jun 5, 2015 at 2:04
  • 1
    Take a look at the bisect library, bisect.insort() Commented Jun 5, 2015 at 4:32
  • 1
    @HaiVu From the documentation of bisect.insort(): "Keep in mind that the O(log n) search is dominated by the slow O(n) insertion step." Commented Sep 7, 2021 at 21:12

2 Answers 2

10

Check out the blist module.

https://pypi.python.org/pypi/blist/

It claims O(log n) insertion.

usage:

x = #list contents y = blist(x) y.insert(index, object) #now works in O(log n) 
Sign up to request clarification or add additional context in comments.

1 Comment

This library is also very efficient for inserting/deleting at random locations in unsorted lists (where the order matters).
6

A shout out to sortedcontainers.SortedList. This will keep your list in order automatically, with a fast insert time.

from sortedcontainers import SortedList mylist = SortedList([1, 2, 4, 5]) mylist.add(3) mylist #>>> SortedList([1, 2, 3, 4, 5], load=1000) 

SortedList insertions are amortized O(sqrt n), or O(cbrt n) with different choices of parameters, but it scales better than blist, which is O(log n), because the constants are much better. There is a very in-depth look at performance on their website.

Alternatively, you might be wanting a priority queue in which case you can get potentially-faster inserts with the heapq module.

3 Comments

The O(log n) part is not strictly true. sortedcontainers.SortedList stores data in balanced lists of lists and exploits data locality achieving faster amortized times. Big-O insert complexity is, strictly speaking, O(n^2), but due to large "load factor", amortized insert complexity is ~ n^(1/3).
@randomir Thanks, this is actually something I was aware of, just not at the time I wrote this. My post is fixed now.
Still a good answer, though (I wasn't aware of sortedcontainers, probably because I didn't need them until today :)).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.