What is the time complexity of Python sorted()?

I’ve been working with Python for years, and the time complexity of the sorted() function is something I rely on a lot. In general, it’s O(n log n), where n is the number of elements in the list.

This efficiency is thanks to Python’s use of Timsort—a hybrid algorithm derived from merge sort and insertion sort. Timsort is designed to handle real-world data efficiently, making sorted() a solid choice for most scenarios.