Advance in Python Sorting in incredible ways!

Rajan Shukla
5 min readDec 19, 2022

Let’s start the blog with some python Fun :)

Why was the Python programmer tired when he got home?

Ans: Because he’d had too many loops.

Let’s understand the importance of sorting in programming.

Sorting algorithms are a popular subject for jokes and memes in the programming community because they can be complex and challenging to understand and implement. Some common themes in sorting algorithm memes include the time and effort required to implement different algorithms, the trade-offs between efficiency and simplicity, and the different approaches that can be taken to solve sorting problems. Sorting algorithms also have a reputation for being used as a “filter” for programming job interviews, so they are a topic of interest for many programmers.

Sorting is an essential concept in programming because it allows you to organize and rearrange data in a specific order. There are many situations in which you might need to sort data, such as:

  1. Searching and retrieving data: When you have a large amount of data, sorting it can make it easier to search and retrieve specific pieces of information. For example, if you have a list of names, sorting the list alphabetically can help you quickly find a specific name.
  2. Analyzing and visualizing data: Sorting data can also make it easier to analyze and visualize trends and patterns. For example, if you have a large dataset of sales data, sorting the data by date can help you see how sales have changed over time.
  3. Simplifying data processing: In many cases, sorting data can make it easier to process and manipulate. For example, if you have a list of numbers that you need to sum, sorting the list in ascending order can make it easier to perform the calculation.

In short, sorting is an essential tool that can help you make sense of large amounts of data and more easily accomplish your goals as a programmer.

One thing that sets Python’s sorting functionality apart from other languages is the wide range of options it provides for customizing the sorting process. Some of the unique features of Python’s sorting functionality include:

  1. Support for multiple data types: Python’s sorting functions can be used to sort lists of any data type, including numbers, strings, and tuples. This is because Python’s sorting functions use a stable, two-pass sorting algorithm called Timsort that is able to handle a wide range of data types.
  2. Custom sorting with the key parameter: Python's sorting functions allow you to specify a key function that takes an element as input and returns a value that will be used to determine the sort order. This allows you to sort elements based on any criterion you choose.
  3. Stable sorting: By default, Python’s sorting functions use an unstable sort algorithm, which means that the order of elements that compare as equal may not be preserved in the sorted array. However, Python’s sort the method allows you to specify a stable sort algorithm (such as mergesort) if you need to maintain the relative order of equal elements.
  4. Advanced sorting with the natsort library: The natsort the library provides natural sorting functionality, which allows you to sort strings in a way that respects the order of numbers within the strings. This can be useful for sorting lists of filenames or other strings that contain numbers.

Overall, Python’s sorting functionality is highly flexible and customizable, which makes it a powerful tool for a wide range of applications.

Here is an example of how to sort an array in Python using the built-in sorted function:

# Initialize an array
my_array = [3, 8, 1, 5, 9, 2]

# Sort the array in ascending order
sorted_array = sorted(my_array)

# Print the sorted array
print(sorted_array)

The output of this code will be: [1, 2, 3, 5, 8, 9]

Alternatively, you can use the sort method of the list object to sort an array in place, without creating a new sorted array:

# Initialize an array
my_array = [3, 8, 1, 5, 9, 2]

# Sort the array in ascending order
my_array.sort()

# Print the sorted array
print(my_array)

The output of this code will be the same as the previous example: [1, 2, 3, 5, 8, 9]

You can also specify a key function to control how the elements of the array are compared, or a reverse flag to sort the array in descending order. For example:

# Initialize an array
my_array = [3, 8, 1, 5, 9, 2]

# Sort the array in descending order using the absolute value of each element as the key
sorted_array = sorted(my_array, key=abs, reverse=True)

# Print the sorted array
print(sorted_array)

The output of this code will be: [9, 8, 5, 3, 2, 1]

Here are some examples of more advanced sorting techniques in Python:

Custom sorting with the key parameter: You can use the key parameter of the sorted function or the sort method to specify a function that takes an element as input and returns a value that will be used to determine the sort order. For example:

# Initialize an array of tuples
my_array = [('Alice', 25), ('Bob', 30), ('Charlie', 20)]

# Sort the array by age, in ascending order
sorted_array = sorted(my_array, key=lambda x: x[1])

# Print the sorted array
print(sorted_array)

The output of this code will be: [('Charlie', 20), ('Alice', 25), ('Bob', 30)]

Sorting with a stable sort algorithm: By default, Python’s sorting functions use a fast, unstable sort algorithm called Timsort. This means that the order of elements that compare as equal may not be preserved in the sorted array. If you need to maintain the relative order of equal elements, you can use a stable sort algorithm by specifying the mergesort algorithm as the sort method's key parameter. For example:

# Initialize an array
my_array = [3, 8, 1, 5, 9, 2]

# Sort the array in ascending order using the mergesort algorithm
my_array.sort(key=lambda x: x, algorithm='mergesort')

# Print the sorted array
print(my_array)
  1. Sorting with the operator module: The operator the module provides a set of functions that can be used as the key parameter for sorting. For example, you can use the itemgetter function to sort a list of tuples by a specific element:
from operator import itemgetter

# Initialize an array of tuples
my_array = [('Alice', 25), ('Bob', 30), ('Charlie', 20)]

# Sort the array by name, in ascending order
sorted_array = sorted(my_array, key=itemgetter(0))

# Print the sorted array
print(sorted_array)

The output of this code will be: [('Alice', 25), ('Bob', 30), ('Charlie', 20)]

Sorting with the natsort library: The natsort the library provides natural sorting functionality, which allows you to sort strings in a way that respects the order of numbers within the strings. For example:

!pip install natsort

import natsort

# Initialize an array
my_array = ['item2', 'item10', 'item1']

# Sort the array using natural sorting
sorted_array = natsort.natsorted(my_array)

# Print the sorted array
print(sorted_array)

The output of this code will be: ['item1', 'item2', 'item10']

These are just a few examples of the many advanced sorting techniques available in Python. You can find more information in the Python documentation and online resources.

At the end

Let me ask this question Why was the programmer’s code so slow?

Ans Because he was using bubble sort to sort his linked list.

--

--