Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ def bubble(data: List[Any], comparator: Callable[[Any, Any], bool]) -> List[Any]
Returns:
List[Any]: A new sorted list.
"""
pass
arr = data.copy() # Do not modify original list
n = len(arr)

for i in range(n):
swapped = False
for j in range(0, n - i - 1):
# If elements are out of order per comparator, swap
if not comparator(arr[j], arr[j + 1]):
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
# Optimization: stop if already sorted
if not swapped:
break
return arr

@staticmethod
def sort(data: List[Any], comparator: Callable[[Any, Any], bool], method: str = "merge") -> List[Any]:
Expand Down