Conversation
…o bst Merging branch bst.
…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
src/merge_sort.py
Outdated
| @@ -0,0 +1,78 @@ | |||
| """Merge Sort Module.""" | |||
|
|
|||
| # MERGE SORT (MS) | |||
There was a problem hiding this comment.
This style of commenting at the top should be contained in the upper triple quotes area:
"""Merge Sort Module.
blah blah blah...
"""
src/merge_sort.py
Outdated
| msl1 = merge_sort(msl1) | ||
| msl2 = merge_sort(msl2) | ||
|
|
||
| def _merge(msla, mslb): |
There was a problem hiding this comment.
msla? perhaps you could use better names here
src/merge_sort.py
Outdated
| # URL: | ||
|
|
||
|
|
||
| def merge_sort(msl): |
There was a problem hiding this comment.
msl? why not list_to_sort, or the_list even?
There was a problem hiding this comment.
msl : merge sort list
src/merge_sort.py
Outdated
| return _merge(msl1, msl2) | ||
|
|
||
|
|
||
| def _random_list(): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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]
src/merge_sort.py
Outdated
| sorted_list = [] | ||
| while len(msla) and len(mslb): | ||
| if msla[0] < mslb[0]: | ||
| low = msla.pop(0) |
There was a problem hiding this comment.
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
|
|
||
| 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)) | ||
|
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
what is the worst case? was trying to figure that out.
src/merge_sort.py
Outdated
|
|
||
| def merge_sort(msl): | ||
| """Merge sort method.""" | ||
| if len(msl) == 1 or not msl: |
No description provided.