Junior — Middle
What sorting algorithm does Python use by default when sorting lists?
sobes.tech AI
Answer from AI
Python uses the Timsort algorithm by default for sorting lists. It is a hybrid sorting algorithm that combines merge sort and insertion sort methods.
Features of Timsort:
- Optimized for real-world data, often containing already sorted segments.
- Stable (maintains the order of equal elements).
- Efficient in terms of time and memory.
Example of usage:
lst = [5, 3, 1, 4, 2]
lst.sort()
print(lst) # [1, 2, 3, 4, 5]