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
Article Tags :
Sorting
Practice Tags :
Sorting
Read Full Article
Python List Sort Key
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).
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:
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:
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: