What is the time complexity of list sort function in Python?

Sorting Algorithms in Python

by Santiago Valdarrama intermediate python

Mark as Completed

Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Introduction to Sorting Algorithms in Python

Sorting is a basic building block that many other algorithms are built upon. It’s related to several exciting ideas that you’ll see throughout your programming career. Understanding how sorting algorithms in Python work behind the scenes is a fundamental step toward implementing correct and efficient algorithms that solve real-world problems.

In this tutorial, you’ll learn:

  • How different sorting algorithms in Python work and how they compare under different circumstances
  • How Python’s built-in sort functionality works behind the scenes
  • How different computer science concepts like recursion and divide and conquer apply to sorting
  • How to measure the efficiency of an algorithm using Big O notation and Python’s timeit module

By the end of this tutorial, you’ll understand sorting algorithms from both a theoretical and a practical standpoint. More importantly, you’ll have a deeper understanding of different algorithm design techniques that you can apply to other areas of your work. Let’s get started!

Free Download: Get a sample chapter from Python Tricks: The Book that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.

Time Complexities of all Sorting Algorithms

Efficiency of an algorithm depends on two parameters:

1. Time Complexity

2. Space Complexity

Time Complexity: Time Complexity is defined as the number of times a particular instruction set is executed rather than the total time is taken. It is because the total time took also depends on some external factors like the compiler used, processor’s speed, etc.

Space Complexity: Space Complexity is the total memory space required by the program for its execution.



Both are calculated as the function of input size(n).

One important thing here is that in spite of these parameters the efficiency of an algorithm also depends upon the nature and size of the input.

Following is a quick revision sheet that you may refer at the last minute

Algorithm Time Complexity
Best Average Worst
Selection Sort Ω(n^2) θ(n^2) O(n^2)
Bubble Sort Ω(n) θ(n^2) O(n^2)
Insertion Sort Ω(n) θ(n^2) O(n^2)
Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2)
Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
Bucket Sort Ω(n+k) θ(n+k) O(n^2)
Radix Sort Ω(nk) θ(nk) O(nk)
Count Sort Ω(n+k) θ(n+k) O(n+k)

Also, see:

  • Searching and Sorting articles
  • Previous year GATE Questions on Sorting

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

What is the time complexity of list sort function in Python?

Article Tags :

Sorting

Practice Tags :

Sorting

Python List Sort Key

What is the time complexity of list sort function in Python?

The list.sort() method takes another function as an optional key argument that allows you to modify the default sorting behavior. The key function is then called on each list element and returns another value based on which the sorting is done. Hence, the key function takes one input argument (a list element) and returns one output value (a value that can be compared).

Here’s an example:

>>> lst = [(1,2), (3,2), (3,3), (1,0), (0,1), (4,2), (1,1), (0,2), (0,0)] >>> lst.sort() >>> lst [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (3, 2), (3, 3), (4, 2)] >>> lst.sort(key=lambda x: x[0]) >>> lst [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (3, 2), (3, 3), (4, 2)] >>> lst.sort(key=lambda x: x[1]) >>> lst [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2), (3, 2), (4, 2), (3, 3)]

You can see that in the first two examples, the list is sorted according to the first tuple value first. In the third example, the list is sorted according to the second tuple value first. You achieve this by defining a key function key=lambda x: x[1] that takes one list element x (a tuple) as an argument and transforms it into a comparable value x[1] (the second tuple value).

Related article:

  • An Introduction to Lambda Functions in Python

Python List Sort Itemgetter

You can use any function as a key function that transforms one element into another (comparable) element.

For example, it’s common to use the itemgetter() function from the operator module to access the i-th value of an iterable:

>>> from operator import itemgetter >>> customers = [('alice', 1000), ('bob', 100), ('frank', 10)] >>> customers.sort(key=itemgetter(1)) [('frank', 10), ('bob', 100), ('alice', 1000)]

The itemgetter() function does exactly the same as the lambda function in the previous example: it returns the second tuple value and uses it as a basis for comparison.

Python List Sort with Two Keys

How to sort a list with two keys? For example, you have a list of tuples [(1,2), (3,2), (3,3), (1,0), (0,1), (4,2), (1,1), (0,2), (0,0)] and you want to sort after the second tuple value first. But if there’s a tie (e.g. (1,2) and (3,2)), you want to sort after the first tuple value. How can you do that?

Per default, Python sorts tuples lexicographically—the first tuple value is considered first. Only if there’s a tie, it takes the second tuple value and so on.

So to sort with “two keys”, you can define a key function that returns a tuple rather than only a single tuple value. Here’s an example:

>>> lst = [(1,2), (3,2), (3,3), (1,0), (0,1), (4,2), (1,1), (0,2), (0,0)] >>> lst.sort(key=lambda x: (x[1], x[0])) >>> lst [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2), (3, 2), (4, 2), (3, 3)]

The second tuple value takes precedence over the first tuple value.

Python List Sort with Multiple Keys

How to sort a list with multiple keys? For example, you have a list of tuples [(1,1,2), (0,0,1), (0,1,0), (0,1,2), (1,4,0)] and you want to sort after the second tuple value first. But if there’s a tie (e.g. (0,1,0) and (1,1,2)), you want to sort after the third tuple value. If there’s another tie, you want to sort after the first tuple value. How can you do that?

Per default, Python sorts tuples lexicographically—the first tuple value is considered first. Only if there’s a tie, it takes the second tuple value and so on.

So to sort with “two keys”, you can define a key function that returns a tuple rather than only a single tuple value. Here’s an example:

>>> lst = [(1,1,2), (0,0,1), (0,1,0), (0,1,2), (1,4,0)] >>> lst.sort() >>> lst [(0, 0, 1), (0, 1, 0), (0, 1, 2), (1, 1, 2), (1, 4, 0)] >>> lst.sort(key=lambda x: (x[1],x[2],x[0])) >>> lst [(0, 0, 1), (0, 1, 0), (0, 1, 2), (1, 1, 2), (1, 4, 0)]

The second tuple value takes precedence over the third tuple value. And the third tuple value takes precedence over the first tuple value.

“time complexity of using list.sort” Code Answer


python sort complexity

python by Santino

What is the time complexity of list sort function in Python?
on Mar 20 2021 Donate Comment

3

Add a Grepper Answer


  • fastest sort python
  • item[0]: (i + 1) * 2 for i, item in (sort_loc)
  • sorting algorithms in python
  • python list index time complexity
  • python min function time complexity
  • python series sort
  • minimumm sort tryed python
  • append time complexity python
  • counter python time complexity
  • set time complexity python

  • python sort time complexity
  • python sorted time complexity
  • python sort function time complexity
  • sort function in python time complexity
  • sort python time complexity
  • time complexity of sorted python
  • sorted in python time complexity
  • sorted() python time complexity
  • python sort method time complexity
  • sort python space complexity
  • python sort() complexity
  • sorting algorithms python time complexity
  • complexity of python sort
  • python .sort() time complexity
  • sort python complexity
  • space complexity python sort
  • time complexity of python sorted function
  • all sorting algorithms in python with time complexity
  • list sort python complexity
  • python built in sort complexity
  • python sorted function complexity
  • complexity of python sorted
  • what is the complexity of the sort function in python
  • complexity of sort python
  • what is the time complexity of python's built-in sort function
  • what is python sort complexity
  • list sort python space complexity
  • sort algorithm complexity python
  • time complexity of python .sorted
  • using sort and then search in python time complexity
  • python sorting complexity
  • .sort space complexity in python
  • sorted list python time complexity
  • time complexity big o of sort function in python
  • sort set list python time complexity
  • time complexity of sorting algorithms in python
  • complexity of arr sort python
  • time complexity of sorted function python
  • complexity of sorting algorithms python
  • sort function complexity python
  • complexity of sorted in python
  • time complexity python list sort
  • sort in python less time complexity
  • what is complexity of sort() in python
  • what is time complexity and space complexity for sort menthod in python
  • time complexity python sort
  • python3 .sort() time complexity
  • python.sort() time complexity
  • python .sort complexity
  • python sort reverse time complexity
  • time complexity of using list.sort
  • python sort function space complexity
  • sort ()in python time complexity
  • inbuilt sort function in python time complexity
  • tim sort python time complexity
  • sorted time complexity python\
  • python sorted list time complexity
  • what time complexity is sort python
  • sorting in least time complexity python
  • time complexity of .sort python
  • sort() method python time complexity
  • sort time complexity python
  • sorted python time complexity
  • python sort complexity
  • sorted time complexity python
  • time complexity of sort() in python
  • time complexity of python sort
  • python sort space complexity
  • sorted function in python time complexity
  • sort in python complexity
  • python sorting without function in n time complexity
  • time complexity of sort python
  • sorted function time complexity python
  • what is the time complexity of sort function in python
  • sort() python complexity
  • python sorted complexity
  • python array sort time complexity
  • space complexity of python sort
  • python inbuilt sort function complexity
  • complexity of sort in python
  • time complexity array.sort() python
  • complexity of sorted function in python
  • time complexity of sorting a sorted list in python
  • sort a sorted list python time complexity
  • complexity of .sort python
  • sort method complexity python
  • list sort time complexity
  • python sort time and space complexity
  • time complexity python sort functon
  • sort elements in list python complexity
  • time complexity sorting python
  • python sorted space complexity
  • complexity of sorted python
  • sort a list python without using sort on complexity
  • time complexity big o of sort() in python
  • complexity of sort function in python
  • time complexity of python sort function
  • python list sort complexity
  • python string sort function time complexity
  • time complexity for sort and sorted python
  • built in sort python time complexity
  • sort in python space complexity
  • time complexity of .sort() in python
  • python sort_values time complexity
  • sort() complexity python
  • time complexity sorted function in python
  • python sort algorithm complexity
  • python time complexity of sorted()
  • python .sort() complexity
  • python count time complexity
  • sort and sorted in python time complexity
  • sorted function python complexity
  • sorted time complexity python
  • sorting an array in python time complexity
  • python array sort time complexity\
  • what sort in python is used time complexity
  • python sorting algorithm complexity
  • time complexity of sorted() python
  • python array sort complexity
  • .sort time complexity python
  • what is time complexity of sorting algorithms in python
  • sort array in python time complexity
  • sort in python time complexity
  • time complexity of sort function in python
  • time complexity of sort in python
  • python sorted function time complexity
  • python list sort time complexity
  • python sort() time complexity
  • python .sort time complexity
  • sort function in python complexity
  • sort complexity python
  • python sorted() time complexity
  • sorted python complexity
  • sort function time complexity python
  • sorted complexity python
  • list sort time complexity python
  • what is time complexity of sort() function in python
  • space complexity python sort
  • python sort list time complexity
  • space complexity of sort in python
  • sort() time complexity python
  • python timsort complexity
  • sort() time complexity python with search
  • python list sort default algorithm time complexity
  • python list.sort time complexity
  • python sort method space complexity
  • what is the time complexity of python sort
  • time complexity to sort in python
  • list sort complexity in python
  • python partition sort complexity
  • python sort function complexity
  • python sort() space complexity
  • sort space complexity in python
  • python list sort() function time complexity
  • how to sort a list in python on complexity
  • .sort() in python usues what time complexity?
  • sorting algorithms time complexity python
  • sort set time complexity python
  • time complexity in python sorting
  • array sort python space complexity
  • sort in python time complexity o(n2)
  • python sort(0 time complexity
  • read python sort method time complexity
  • time complexity of inbuilt sort() in python
  • sort() in python time complexity
  • python list.sort complexity
  • space and time complexity of sort in python
  • what is time complexity of sort function in python
  • is sorting algorithms pyhton time complexity
  • a.sort() python big complexity
  • time complexity of list.sort python
  • sort function python time complexity
  • what is the sort space complexity for python
  • sorted python function time complexity
  • complexity of python .sort function
  • python sorted() time complexity
  • list sort complexity python
  • sorted() complexity in python
  • how to sort a list python o(1) time complexity
  • python inbuilt sort time complexity
  • python .sort() space complexity
  • sorting with time complexity o(n) in python