Fastest sorting algorithm in Python

Python uses an algorithm called Timsort for its built-in sort method for lists and the sorted function. Timsort is derived from merge sort and insertion sort. It has an average case time complexity of O(n log n), which is the best you can achieve for comparison-based sorting algorithms.

While Timsort is highly efficient for a broad range of input datasets, it’s worth mentioning that “fastest” can be relative. Depending on the specific nature of the data you’re working with and the constraints of your problem, other sorting algorithms might be more efficient in certain cases.

However, for most general purposes, Timsort is a great choice because it’s adaptive, stable, and has good average-case performance.

Below is a tutorial that explains how to use Python’s built-in sorting methods.

Fastest Sorting Algorithm in Python

Using Python’s Built-in Sorting Functions

sorted()

The sorted() function can be used to sort any iterable and returns a new sorted list from the elements of the given iterable.

# Example: Sorting a list of numbers
numbers = [2, 4, 1, 5, 3]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # Output: [1, 2, 3, 4, 5]

# Example: Sorting a list of strings
words = ["apple", "banana", "cherry"]
sorted_words = sorted(words)
print(sorted_words)  # Output: ['apple', 'banana', 'cherry']

You can see the output like below:

fastest sorting algorithm python
fastest sorting algorithm python

.sort()

The sort() method sorts the elements of a list in-place and returns None. This means that it modifies the original list and doesn’t create a new list.

# Example: Sorting a list of numbers in-place
numbers = [2, 4, 1, 5, 3]
numbers.sort()
print(numbers)  # Output: [1, 2, 3, 4, 5]

Check out the output like below:

best sorting algorithm python
best sorting algorithm python

Custom Sorting

Both sort() and sorted() can take two optional arguments: key and reverse.

  • key: This takes a function as an argument, which would be used for custom sorting. For instance, if you want to sort based on the length of strings.
  • reverse: This can be set to True if you want the sorting to be done in descending order.
# Example: Sorting a list of strings based on length
words = ["apple", "banana", "cherry", "date"]
sorted_words = sorted(words, key=len)
print(sorted_words)  # Output: ['date', 'apple', 'banana', 'cherry']

# Example: Sorting a list of numbers in descending order
numbers = [2, 4, 1, 5, 3]
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)  # Output: [5, 4, 3, 2, 1]

You can see the output like below:

best sorting algorithm in python
best sorting algorithm in python

Conclusion

For most cases, using Python’s built-in sorting functions sorted() or the .sort() method should be sufficient and is among the fastest options available due to the highly optimized Timsort algorithm. It’s generally a good idea to start with these and consider other options only if you find that they do not meet your specific needs.

READ:  How to Check Armstrong Number in Python [with 5 Methods]

You may also like: