Insert sort - #21
Conversation
| import timeit | ||
|
|
||
|
|
||
| best_case = [x for x in range(0, 1000)] |
There was a problem hiding this comment.
If you declare constants in the global scope, follow Python convention by making the variable names ALL_UPPER_CASE
| """Sort a list by inserting values in order after comparing each value.""" | ||
| if len(l) <= 1: | ||
| return | ||
| else: |
There was a problem hiding this comment.
This else statement is unnecessary, since the if conditional will return out of the function when True. Whenever possible, as in this case, it is preferable to avoid indented blocks so your code looks a little cleaner.
| if len(l) <= 1: | ||
| return | ||
| else: | ||
| for idx in range(1, len(l)): |
There was a problem hiding this comment.
Good insertion sort implementation! Only improvement I would want to see is to make a more meaningful/readable variable name than just "l" for the input list.
| l[spot] = cur | ||
|
|
||
|
|
||
| if __name__ == '__main__': |
There was a problem hiding this comment.
What's weird is that this looks correct and your best case/ worse case look like they are what they should be, but I'm getting the same time performance for both cases. Are you getting the same?
|
|
||
|
|
||
| TEST_LIST = [ | ||
| [], [8] |
There was a problem hiding this comment.
Good edge cases. You should also make sure to include cases with duplicate values. Not as important for this assignment, more so for future sorts.
|
|
||
| def test_best_case(): | ||
| from insert_sort import best_case | ||
| assert type(best_case) is list |
There was a problem hiding this comment.
What are you actually testing for here?
There was a problem hiding this comment.
making sure my best case and worst case variables are actually lists, I've changed it to isinstance
No description provided.