Introduction to Sorting Algorithms in Python
Sorting is a way to rearrange or reorder a data in ascending or descending order. Sometimes we need to sort a data to process it quickly or use it efficiently. Depends on various applications and its use, there are many things that we need to sort in data, like roll numbers in ascending order of merit list, records in database, etc.
Over the years, there are many sorting algorithms are developed to perform sorting in a list. In Python, we can use these sorting algorithms to sort data in list depends upon particular situation. Selection of proper sorting algorithm depends on two parameters: Time Complexity and Space Complexity. Time Complexity is the time require to execute particular algorithm and Space complexity is space require to execute particular algorithm.
There are many sorting techniques available and almost in all sorting techniques one thing is common which is “Swapping”. So basically, we swap elements and that’s how we make sure that all the elements are sorted. So swapping is one of the most important concepts in sorting and we need to understand it first. So, we take one example to understand it. Let us have two variables A and B and we want to swap values of these two variables. Basically, we take third variable, let say Temp to swap values of A and B as follows.
But in python this task it very easy due to the ability of multiple assignments in python. Multiple assignments is possible in python which makes swapping very easy. We can swap values of A and B in python as follows.
This concept of python uses to optimize the code and reduce the number of lines in code.
In this article, I have list various popular sorting algorithms in python and next we will explore these algorithms and will understand how they are working and how to code them in python.
Inbuilt Python Sort Functions
Apart from above popular sorting algorithms, we can also use python’s inbuilt sort functions to sort a list.
Python have inbuilt method sort() to sort the list.
Above code sort the list in ascending order. If you want to sort the list in descending order then we can set reverse parameter to True as follows:
Moreover, Python have inbuilt function sorted() which creates a new sorted list.
Same way, if we want to create new sorted list in descending order then we can set reverse parameter of sorted function to True as follows:
Other Important Topics:
- Python and Other Object-Oriented Programming Languages
- Python 2 vs Python 3
- Introduction to Python Programming
- The Basic Elements of Python Programming
- Branching, Indentation, Looping and Control Structure in Python
- Indexing & Slicing of Strings and Capturing Inputs
- Built-in Data Types and Functions in Python
- Specifications, Global Variables, Modules and Packages in Python
- Working with Files in Python
- Strings in Python
- Lists in Python
- Tuples in Python
- Dictionaries in Python
- Mutable and Immutable Python Objects
- Functions as Objects, map(), filter() and reduce()
- Exception Handling in Python
- Classes and Object Oriented Programming in Python
- Searching Algorithms in Python
- Method Resolution Order (MRO) in Python
- Sorting Algorithms in Python