-1

I have a large number of very small lists I need to sort as quickly as possible. Usually these lists have 2-3 values in them, and the built in sorting methods seem to have too much overhead. Would a simple bubble sort be ok in this situation?

Example, want to sort:

[1, 5]
[4, 2]
[3, 7]
...

To:

[1, 5]
[2, 4]
[3, 7]
...

Right now I'm doing something like this:

def do_something( ilist ):
    ilist = sorted(ilist, reverse = True);
    return ilist;

for i in range(1000000000):
    do_something( [random_num,random_num ] );

Thanks.

7
  • 10
    What makes you think the built in functions have too much overhead? Commented Apr 30, 2014 at 2:19
  • 2
    Python's built-in sort is actually quite fast. It adjusts the algorithm it uses based on the size of the input, and for small lists it'll just end up using insertion sort. It's also implemented in C, so it'll probably be faster than anything you come up with in pure Python. See en.wikipedia.org/wiki/Timsort Commented Apr 30, 2014 at 2:21
  • 1
    Rule: never use bubble sort. Commented Apr 30, 2014 at 2:22
  • possible duplicate of Python sort() first element of list Commented Apr 30, 2014 at 2:24
  • My code is like 7 times slower when I sort, which seems like a lot since about half of the lists are already sorted. I just need a method to swap the elements if they're out of order, which doesn't seem like an exponential operation Commented Apr 30, 2014 at 2:36

4 Answers 4

2

Yes. If the list of the lists has always 2 element. It's faster to use like > operator than using sorted.

[(i[1], i[0]) if i[0]>i[1] else i for i in lst]

Time:

lst = [(0, 9),
       (1, 8),
       (2, 7),
       (3, 6),
       (4, 5),
       (5, 4),
       (6, 3),
       (7, 2),
       (8, 1),
       (9, 0)]

%timeit [(i[1], i[0]) if i[0]>i[1] else i for i in lst]
1000000 loops, best of 3: 1.96 us per loop

%timeit [sorted(i) for i in lst]
100000 loops, best of 3: 5.87 us per loop

In your case, you said your list has 2 or 3 elements. So your sort function look like this.

def sort_few(lst):
    if len(lst)==2:
        if lst[0] > lst[1]:
            return (lst[1], lst[0])
        else:
            return lst
    else:
        if lst[0] > lst[1]:
            if lst[1] > lst[2]:
                return (lst[2], lst[1], lst[0])
            else:
                return (lst[1], lst[2], lst[0])
        elif lst[1] > lst[2]:
            if lst[2] > lst[0]:
                return (lst[0], lst[2], lst[1])
            else:
                return (lst[2], lst[0], lst[1])
        elif lst[2] > lst[0]:
            if lst[0] > lst[1]:
                return (lst[1], lst[0], lst[2])
            else:
                return lst

Time:

lst = [(1, 2, 3),
       (1, 3, 2),
       (2, 1, 3),
       (2, 3, 1),
       (3, 1, 2),
       (3, 2, 1),
       (1, 2, 3),
       (1, 3, 2),
       (2, 1, 3),
       (2, 3, 1),
       (3, 1, 2),
       (3, 2, 1)]


%timeit [sort_few(i) for i in lst]
100000 loops, best of 3: 6.3 us per loop

%timeit [sorted(i) for i in lst]
100000 loops, best of 3: 7.32 us per loop

So it's faster to use sort_few than sorted if there are a 2 or 3 element in the list.

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

Comments

0

Using sorted() is pretty efficient:

>>> list_of_lists = [[1, 5], [4, 2], [3, 7]]
>>> sorted_lol = [sorted(sub_list) for sub_list in list_of_lists]
>>> sorted_lol
[[1, 5], [2, 4], [3, 7]]

1 Comment

Thanks, but its not sublists. It's a function that sorts a list of size 2... 100000000 times or however many are needed.
0
values = [{1, 5},{4, 2},{3, 7}]
print(sorted(values))

Result:

[set([1, 5]), set([2, 4]), set([3, 7])]

1 Comment

He wants to sort sub-lists, and sets are unordered, so I don't see how this answers his question at all.
0

If it's only two elements:

f = lambda x: x if x[0] < x[1] else [x[1],x[0]]

then

x = [5,4]
f(x)

This would return '[4,5]'

x = [4,5]
f(x)

Also returns [4,5]

If it's more than two this doesn't work, though you could special case 3. This just doing a compare and swap instead of calling a full sort function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.