2

Possible Duplicate:
About python’s built in sort() method

Which of the sorting algorithms does the sort() method use to sort a list of numbers? How can I prove it?

seq = list_of_numbers
seq.sort()
6
  • As a side note, You probably mean, "Which algorithm does cpython's sort method use". A python implementation is free to use whatever sorting algorithm that it wants provided that the algorithm is stable. Commented Jan 9, 2013 at 13:33
  • @mgilson: Jython uses Collections.sort() (so presumably that's also TimSort). Looking for IronPython now.. Commented Jan 9, 2013 at 13:42
  • @MartijnPieters -- Timsort is good. I suspect that they all use it. I just wanted to point out (again) that cpython is not python. It's an implementation and OP is asking about an implementation detail. Commented Jan 9, 2013 at 13:49
  • 1
    @mgilson: Yup, I know; just tickled into looking for the implementations. I can honestly say that "I was there" when Tim invented TimSort (we were working at the same company at the time), so I feel a certain affinity. :-) Commented Jan 9, 2013 at 13:53
  • @mgilson: after some distractions, found the IronPython list implementation. It uses merge sort (I am all disappointed now). Commented Jan 9, 2013 at 14:16

3 Answers 3

10

It uses TimSort, an algorithm developed for Python by Tim Peters (of Zen of Python fame).

It is a hybrid of Merge and Insertion sort, and now also in use in Java and Android. The Python source code includes a more detailed description. You'll find the implementation in the listobject.c C source.

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

Comments

1

The easiest way to determine the sorting algorithm and to prove you're correct is look at the source.

Comments

0

This may enlight you. http://www.daniweb.com/software-development/python/code/216689/sorting-algorithms-in-python

You can prove it by showing the c code under the hood.

This is almost your same question. About Python's built in sort() method

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.