135

What algorithm is the built in sort() method in Python using? Is it possible to have a look at the code for that method?

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 12
    Of course it's possible to look at the code for the method - Python is an open-source project. The method is probably implemented in C, however, so you'll have to know a bit about C to make any sense of it. – Chris Lutz Oct 04 '09 at 20:50
  • Does the version matter? – meder omuraliev Oct 04 '09 at 20:50
  • @melder: No =) I just want to have a look at a pro algorithm :P @chris: how? –  Oct 04 '09 at 20:51
  • 3
    Download the source code to the Python interpreter. I don't know where they implement the `sort()` method, or what the formatting to the interpreter is, but it's got to be in there somewhere, and I bet it's implemented in C for speed concerns. – Chris Lutz Oct 04 '09 at 20:54
  • [Here](http://stackoverflow.com/questions/36139/how-do-i-sort-a-list-of-strings-in-python) is an example of it being used – Hari Ganesan Feb 04 '14 at 17:21

2 Answers2

147

Sure! The code's here, starting with function islt and proceeding for QUITE a while;-). As Chris's comment suggests, it's C code. You'll also want to read this text file for a textual explanation, results, etc etc.

If you prefer reading Java code than C code, you could look at Joshua Bloch's implementation of timsort in and for Java (Joshua's also the guy who implemented, in 1997, the modified mergesort that's still used in Java, and one can hope that Java will eventually switch to his recent port of timsort).

Some explanation of the Java port of timsort is here, the diff is here (with pointers to all needed files), the key file is here -- FWIW, while I'm a better C programmer than Java programmer, in this case I find Joshua's Java code more readable overall than Tim's C code;-).

Stephen Fuhry
  • 12,624
  • 6
  • 56
  • 55
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 4
    @Chris, "Browse Python sources" is a shortcut in all of my browsers' bookmark bars -- it points to http://svn.python.org/view/python/trunk/ ;-). – Alex Martelli Oct 04 '09 at 21:05
  • 1
    I want to know what the function `list_ass_item()` does. :) – Chris Lutz Oct 04 '09 at 21:10
  • 2
    Performs assignment to an item of the list (just like list_ass_slice performs assignment to a slice of the list), nothing to do with sorting. I guess the abbreviation of "assignment" makes the name funny... – Alex Martelli Oct 04 '09 at 23:22
  • 3
    [The current version](http://hg.python.org/cpython/file/default/Objects/listsort.txt) of `listsort.txt` adds some notes that address common confusions. – Tim Peters Dec 18 '13 at 10:03
  • What are the theoretical time and space complexities of timsort? – Fırat Kıyak Aug 21 '23 at 12:22
11

In early versions of Python, the sort function implemented a modified version of quicksort. However, in 2.3 this was replaced with an adaptive mergesort algorithm, in order to provide a stable sort by default.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Silfverstrom
  • 28,292
  • 6
  • 45
  • 57
  • 2
    quicksort isn't 'deemed unstable' - by the commonly used definitions, quicksort is unstable - that is the original ordering of two objects is not preserved, if they are equal during this sort. Having an unstable sort algorithm means that you have to come up with all sorts of 'magic' to sensibly sort data with multiple criteria, rather than then simply being able to chain sort calls - in timsort if you want to sort by D.O.B and then name, you simply sort by name first, and then D.O.B. You can't do that with quicksort – Tony Suffolk 66 Jul 30 '20 at 06:19
  • 2
    @TonySuffolk66 I think the author of this answer may have, at the time, been reading the documentation and misunderstood "stable" as referring to software quality - rather than to a property of sorting algorithms. I edited the answer so that it is clear about the meaning, and added a reference link for that concept. – Karl Knechtel Jan 14 '23 at 09:14