Sorting Algorithms in Pythonby Santiago Valdarrama intermediate python Show
Mark as Completed Table of Contents
Tweet Share Email Table of Contents Remove adsWatch 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:
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 AlgorithmsEfficiency 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
Also, see:
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 KeyThe 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:
Python List Sort ItemgetterYou 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 KeysHow 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 KeysHow 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 Answerpython sort complexity python by Santino on Mar 20 2021 Donate Comment3 Add a Grepper Answer Python answers related to “time complexity of using list.sort”
Python queries related to “time complexity of using list.sort”
|