Skip to content

Commit

Permalink
modified the return from both main and helper. (keon#886)
Browse files Browse the repository at this point in the history
It looks like there is no need to create a copy, we can replace the new elements inplace.
  • Loading branch information
patilabhay679 authored Mar 8, 2023
1 parent 2a129ba commit fd86fd1
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions algorithms/sort/merge_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def merge_sort(arr):
left, right = merge_sort(arr[:mid]), merge_sort(arr[mid:])

# Merge each side together
return merge(left, right, arr.copy())
# return merge(left, right, arr.copy()) # changed, no need to copy, mutate inplace.
merge(left,right,arr)
return arr


def merge(left, right, merged):
Expand All @@ -35,4 +37,4 @@ def merge(left, right, merged):
merged[left_cursor + right_cursor] = right[right_cursor]

# Return result
return merged
# return merged # do not return anything, as it is replacing inplace.

0 comments on commit fd86fd1

Please sign in to comment.