Skip to content

started work on merge_sort#23

Open
vbenavente wants to merge 5 commits into
masterfrom
merge_sort
Open

started work on merge_sort#23
vbenavente wants to merge 5 commits into
masterfrom
merge_sort

Conversation

@vbenavente
Copy link
Copy Markdown
Owner

No description provided.

Comment thread src/merge_sort.py
return a_list
left = []
right = []
for i in range(len(a_list)):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unconventional way of splitting a list in half, and I'm loving it. Nice job.

Comment thread src/merge_sort.py Outdated
else:
new_list.append(right[0])
right = right[1:]
while left:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These next 6 lines can be simplified and made much less expensive using the list.extend method, since all you're doing is adding the remaining elements of left, then the remaining elements of right:

new_list.extend(left)
new_list.extend(right)

Comment thread src/merge_sort.py Outdated
while left and right:
if left[0] < right[0]:
new_list.append(left[0])
left = left[1:]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach will work, but will be more expensive than necessary because creating a slice of a list will use an additional O(slice size) time and space.

The canonical merge sort implementation uses two separate index pointers for the left and right lists. You could also create two different generators which you only advance using next() when necessary.

Comment thread src/merge_sort.py
new_list.append(right[right_idx])
right_idx += 1
while left_idx < len(left):
new_list.extend([left[left_idx]])
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really what I had in mind. This isn't really how you use list.extend.

You can do this all in a single line:

new_list.extend(left[left_idx:])
new_list.extend(right[right_idx:])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants