0

There's already a question regarding this, and the answer says that the asymptotic complexity is O(n). But I observed that if an unsorted list is converted into a set, the set can be printed out in a sorted order, which means that at some point in the middle of these operations the list has been sorted. Then, as any comparison sort has the lower bound of Omega(n lg n), the asymptotic complexity of this operation should also be Omega(n lg n). So what exactly is the complexity of this operation?

1
  • Converting a list to a set is still O(n). Printing it out is another operation. Commented Nov 1, 2019 at 10:19

1 Answer 1

1

A set in Python is an unordered collection so any order you see is by chance. As both dict and set are implemented as hash tables in CPython, insertion is average case O(1) and worst case O(N).

So list(set(...)) is always O(N) and set(list(...)) is average case O(N).

You can browse the source code for set here.

Sign up to request clarification or add additional context in comments.

1 Comment

Oops. Seems that the two convertions I had run somehow both returned sorted sets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.