Skip to content

Commit ecbd2bf

Browse files
⚡️ Speed up function sorter by 118,986%
Here's an optimized version of your program. Your existing implementation uses **bubble sort**, which is O(n²). For optimal performance, you should use Python's built-in sorting, which is implemented in C and is much faster (Timsort, O(n log n)). We keep the comments (none in your original code) and keep the function signature unchanged. This version provides a massive speedup for all list sizes. If you don't want to modify the input array, you could use `return sorted(arr)`, but your original bubble sort is in-place, so this preserves that behavior.
1 parent 2ed293e commit ecbd2bf

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import List, Union
2+
3+
4+
def sorter(arr: Union[List[int], List[float]]) -> Union[List[int], List[float]]:
5+
# Use in-place sort for better performance
6+
arr.sort()
7+
return arr

0 commit comments

Comments
 (0)