how to keep a list sorted as you read elements

Question:

What is the efficient way to read elements into a list and keep the list sorted apart from searching the place for a new element in the existing sorted list and inserting in there?

Asked By: vkaul11

||

Answers:

Use a specialised data structure, in Python you have the bisect module at your disposal:

This module provides support for maintaining a list in sorted order without having to sort the list after each insertion. For long lists of items with expensive comparison operations, this can be an improvement over the more common approach. The module is called bisect because it uses a basic bisection algorithm to do its work.

Answered By: Óscar López

You’re looking for the functions in heapq.

This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.

@Aswin’s comment is interesting. If you are sorting each time you insert an item, the call to sort() is O(n) rather than the usual O(n*log(n)). This is due to the way the sort(timsort) is implemented.

However on top of this, you’d need to shift a bunch of elements along the list to make space. This is also O(n), so overall – calling .sort() each time is O(n)

There isn’t a way to keep a sorted list in better than O(n), because this shifting is always needed.

If you don’t need an actual list, the heapq (as mentioned in @Ignacio’s answer) often covers the properties you do need in an efficient manner.

Otherwise you can probably find one of the many tree data structures will suit your cause better than a list.

Answered By: John La Rooy

example with SortedList:

from sortedcontainers import SortedList

sl = SortedList()

sl.add(2)
sl.add(1)

# result sl = [1,2]
 
Answered By: BigChief
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.