Skip to content

Conversation

@chamberi
Copy link
Owner

No description provided.

CCallahanIV and others added 30 commits January 16, 2017 12:44
…o bst

sdf building a binary search tree.
…ctures into traversal-bst

g
g Lines starting with '#' will be ignocquire post_order_trav. red, and an empty message aborts
@@ -0,0 +1,78 @@
"""Merge Sort Module."""

# MERGE SORT (MS)
Copy link

Choose a reason for hiding this comment

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

This style of commenting at the top should be contained in the upper triple quotes area:
"""Merge Sort Module.

blah blah blah...
"""

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed it.

msl1 = merge_sort(msl1)
msl2 = merge_sort(msl2)

def _merge(msla, mslb):
Copy link

Choose a reason for hiding this comment

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

msla? perhaps you could use better names here

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed.

# URL:


def merge_sort(msl):
Copy link

Choose a reason for hiding this comment

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

msl? why not list_to_sort, or the_list even?

Copy link
Collaborator

Choose a reason for hiding this comment

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

msl : merge sort list

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed.

return _merge(msl1, msl2)


def _random_list():
Copy link

Choose a reason for hiding this comment

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

You could one line this. If you are going to bother to make this a function, why not make it so you can pass in an integer n, and gen a list of length n?

Copy link
Collaborator

Choose a reason for hiding this comment

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

so rather than this:

def _random_list():
"""Return a list of random numbers from 0 to 300 of random size less than 300."""
import random
b = random
return b.sample(range(0, 300), 150)

a = _random_list()
r = a[:]
b = sorted(a)
w = b[::-1]


something like this?

z = random

a = z.sample(range(0, 300), 150)
r = a[:]
b = sorted(a)
w = b[::-1]


or

def _random_list(n):
"""Return a list of random numbers from 0 to 300 of size n."""
import random
b = random
return b.sample(range(0, 300), n)

a = _random_list(150)
r = a[:]
b = sorted(a)
w = b[::-1]

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed.

sorted_list = []
while len(msla) and len(mslb):
if msla[0] < mslb[0]:
low = msla.pop(0)
Copy link

Choose a reason for hiding this comment

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

pop(0) totally works, but I would try and avoid it, as it is an O(n) operation, which kills the efficiency of this sorting algorithm

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah good point.

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed.


worst_merge_sort_timed = timeit.repeat(stmt="merge_sort(w)", setup="from merge_sort import merge_sort, w", number=1000, repeat=3)
worst_average_merge_sort_timed = float(sum(worst_merge_sort_timed) / len(worst_merge_sort_timed))

Copy link

Choose a reason for hiding this comment

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

looking at your output, it is clear that the 'worst case' is not the actual worst case. Simply reversing a list is not the worst case for this algorithm.

number of runs: 3
random merge_sort_timed: [0.754100672000277, 0.7524420129998362, 0.7507033059996502]
average: 0.7524153303332545
number of runs: 3
best case merge_sort_timed: [0.5421898399999918, 0.5447728300000563, 0.5409313089999159]
average: 0.5426313263333213
number of runs: 3
worst case merge_sort_timed: [0.5831306340000992, 0.6116077660003612, 0.5884709070001009]
average: 0.5944031023335204

Copy link
Collaborator

Choose a reason for hiding this comment

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

what is the worst case? was trying to figure that out.

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed.


def merge_sort(msl):
"""Merge sort method."""
if len(msl) == 1 or not msl:
Copy link

Choose a reason for hiding this comment

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

len(msl) < 2 would work too

Copy link
Collaborator

Choose a reason for hiding this comment

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

good point.

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed.

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.

5 participants