-
Notifications
You must be signed in to change notification settings - Fork 1
Merge sort #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Merge sort #9
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.
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...
"""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed it.
src/merge_sort.py
Outdated
| msl1 = merge_sort(msl1) | ||
| msl2 = merge_sort(msl2) | ||
|
|
||
| def _merge(msla, mslb): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed.
src/merge_sort.py
Outdated
| # URL: | ||
|
|
||
|
|
||
| def merge_sort(msl): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
msl : merge sort list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed.
src/merge_sort.py
Outdated
| return _merge(msl1, msl2) | ||
|
|
||
|
|
||
| def _random_list(): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed.
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.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah good point.
There was a problem hiding this comment.
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)) | ||
|
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed.
src/merge_sort.py
Outdated
|
|
||
| def merge_sort(msl): | ||
| """Merge sort method.""" | ||
| if len(msl) == 1 or not msl: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed.
No description provided.