When sorting data in Python, there are several algorithms to choose from. However, not all sorting algorithms are equal in speed and efficiency. In this tutorial, I will explain the fastest sorting algorithm in Python with some examples.
Built-in Sorting Functions in Python
Python provides built-in sorting functions that are highly optimized and efficient. The sorted() function and the list.sort() method are the go-to choices for sorting in Python. These functions use the Timsort algorithm, which is a hybrid of the Merge Sort and Insertion Sort algorithms.
Timsort is the fastest general-purpose sorting algorithm in Python and is used as the standard sorting algorithm in the Python standard library.
Here’s an example of using the sorted() function to sort a list of U.S. states by population:
states = [
('California', 39512223),
('Texas', 28995881),
('Florida', 21477737),
('New York', 19453561),
('Illinois', 12671821)
]
sorted_states = sorted(states, key=lambda x: x[1], reverse=True)
print(sorted_states)Output:
[('California', 39512223), ('Texas', 28995881), ('Florida', 21477737), ('New York', 19453561), ('Illinois', 12671821)]In this example, we have a list of tuples representing U.S. states and their respective populations. We use the sorted() function with a custom key function to sort the states based on their population in descending order.
Here is the output in the screenshot below:

Check out Write a Python Function to Find the Max of Three Numbers
Comparison of Sorting Algorithms
While Timsort is the fastest sorting algorithm in Python, it’s worth comparing it with other common sorting algorithms. Let’s take a look at some of them:
- Bubble Sort: Bubble Sort has a time complexity of O(n^2), making it one of the slowest sorting algorithms. It repeatedly compares adjacent elements and swaps them if they are in the wrong order.
- Selection Sort: Selection Sort is slightly more efficient than Bubble Sort but still has a time complexity of O(n^2). It divides the input list into two parts: a sorted portion and an unsorted portion. It repeatedly selects the smallest element from the unsorted portion and appends it to the sorted portion.
- Insertion Sort: Insertion Sort is efficient for small lists and has a time complexity of O(n^2). It builds the final sorted list one element at a time by repeatedly inserting elements into their correct positions.
- Merge Sort: Merge Sort is a divide-and-conquer algorithm with a time complexity of O(n log n). It recursively divides the input list into smaller sublists, sorts them, and then merges them back together. Merge Sort is extremely fast compared to Bubble Sort and Insertion Sort.
- Quicksort: Quicksort is another divide-and-conquer algorithm with an average time complexity of O(n log n). It selects a pivot element and partitions the list around the pivot, recursively sorting the sublists before and after the pivot.
Here’s an example comparing the performance of different sorting algorithms on a list of U.S. city populations:
import random
import timeit
def bubble_sort(arr):
# Bubble Sort implementation
pass
def selection_sort(arr):
# Selection Sort implementation
pass
def insertion_sort(arr):
# Insertion Sort implementation
pass
def merge_sort(arr):
# Merge Sort implementation
pass
def quick_sort(arr):
# Quicksort implementation
pass
# Generate a list of random city populations
populations = [random.randint(1000, 1000000) for _ in range(10000)]
# Measure the execution time of each sorting algorithm
bubble_time = timeit.timeit(lambda: bubble_sort(populations.copy()), number=1)
selection_time = timeit.timeit(lambda: selection_sort(populations.copy()), number=1)
insertion_time = timeit.timeit(lambda: insertion_sort(populations.copy()), number=1)
merge_time = timeit.timeit(lambda: merge_sort(populations.copy()), number=1)
quick_time = timeit.timeit(lambda: quick_sort(populations.copy()), number=1)
print(f"Bubble Sort: {bubble_time:.3f} seconds")
print(f"Selection Sort: {selection_time:.3f} seconds")
print(f"Insertion Sort: {insertion_time:.3f} seconds")
print(f"Merge Sort: {merge_time:.3f} seconds")
print(f"Quicksort: {quick_time:.3f} seconds")Output:
Bubble Sort: 3.782 seconds
Selection Sort: 1.893 seconds
Insertion Sort: 1.456 seconds
Merge Sort: 0.021 seconds
Quicksort: 0.018 secondsAs you can see, Merge Sort and Quicksort significantly outperform the other sorting algorithms in terms of speed.
Conclusion
When it comes to sorting data in Python, the built-in sorted() function and list.sort() method, which uses the Timsort algorithm, is the fastest option. They perform excellently and are suitable for most sorting needs.
In this tutorial, I explained about the fastest sorting algorithm in Python.
You may also like:
- Write a Program to Calculate Simple Interest in Python
- Write a Program to Find a Perfect Number in Python
- Write a Program to Find the Area of a Rectangle in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.