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
38 changes: 37 additions & 1 deletion sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,43 @@ def merge(data: List[Any], comparator: Callable[[Any, Any], bool]) -> List[Any]:
Returns:
List[Any]: A new sorted list.
"""
pass
arr = data[:] # work on a copy to keep immutability
temp = [None] * len(arr) # pre-allocate temp list once (performance gain)

def merge_sort(start: int, end: int):
if end - start <= 1:
return

mid = (start + end) // 2
merge_sort(start, mid)
merge_sort(mid, end)

# Merge two sorted halves (in-place using temp)
i, j, k = start, mid, start
while i < mid and j < end:
if comparator(arr[i], arr[j]):
temp[k] = arr[i]
i += 1
else:
temp[k] = arr[j]
j += 1
k += 1

# Copy remaining elements
while i < mid:
temp[k] = arr[i]
i += 1
k += 1
while j < end:
temp[k] = arr[j]
j += 1
k += 1

# Copy back to arr
arr[start:end] = temp[start:end]

merge_sort(0, len(arr))
return arr

@staticmethod
def insertion(data: List[Any], comparator: Callable[[Any, Any], bool]) -> List[Any]:
Expand Down