0

I was solving a hackerrank challenge.(in Python 3) At one point I needed to sort a list and work with it. (2 <= len(array) <= 10^5) The list is containing unique integers. I used arr.sort(), but I was getting "Time limit exceeded" message Then I found if I use set(arr), it runs fast and passes all test cases. here's is my function for more clarification

def pairs(k, ar):
  ar.sort()
  c=0
  for i in ar:
    if i+k in ar:
      c+=1
  return c

This gets me "Time limit exceeded"

def pairs(k, ar):
  ar=set(ar)
  c=0
  for i in ar:
    if i+k in ar:
      c+=1
  return c

This one with set() runs faster. Why is this set() is faster than sort(). Don't they use same sorting algorithm?

11
  • 4
    @TimRoberts set objects are absolutely not sorted in Python, not even as an implementation detail in any of the major implementations Commented Sep 15, 2021 at 4:51
  • 4
    "Why is this set() is faster than sort(). " It's not either of these calls that is fast or slow here; it's the in operator. set doesn't use a sorting algorithm at all, and produces something completely different. That said, the reason that sorting the list gives you a much slower result than using a set is that sorting the list doesn't help in work at all; the in operator has no way to know that the list has been sorted, and therefore every element is still checked every time. Commented Sep 15, 2021 at 4:51
  • 3
    @RifatBhuiyan yeah, that's completely incorrect. What you are seeing is an artifact of the fact that int objects (mostly) hash to themselves Commented Sep 15, 2021 at 4:53
  • 2
    @KarlKnechtel: Note that a presorted list could gain some speed on containment checks (going from O(n) to O(log n)) by using the bisect module functions. But sorting (O(n log n)) plus repeated binary search (O(log n)) is almost certainly going to be much slower than conversion to set (O(n)) plus repeated set lookup (average case O(1)). But yeah, in on a sorted list and in on an unsorted list have identical performance (only way they'd differ in practice is if the thing being searched for was likely to be bigger or smaller than most elements of the list). Commented Sep 15, 2021 at 4:59
  • 2
    @RifatBhuiyan: == on sets compares them w/o regard to iteration order, so that would be true even if the sets in question had the same values but iterated in different orders. Try it with {-2, -1} and {-1, -2}. They iterate in different orders (because I chose two values that, as an implementation detail of CPython, have identical hashes, so the first seen occupies its desired bucket, while the second hops around to find an open one; other values do this too, they're just a little harder to find), but they're still equal to each other. set doesn't sort, and has no useful ordering. Commented Sep 15, 2021 at 5:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.