-
Notifications
You must be signed in to change notification settings - Fork 3
Insert sort #21
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
Open
vbenavente
wants to merge
7
commits into
master
Choose a base branch
from
insert_sort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Insert sort #21
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c55383
created helper functions for self balancing function
vbenavente b263347
done test
steventhan 25c4984
refactored insert_sort, printing best and worst case scenarios
vbenavente e140a39
made test more meaningful
vbenavente 0a08cc2
removed unecessary else, made global variables all caps
vbenavente 9f98567
changed type to isinstance in test module
vbenavente 3d3ac43
updated readme
vbenavente File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import timeit | ||
|
|
||
|
|
||
| BEST_CASE = [x for x in range(0, 1000)] | ||
| WORST_CASE = BEST_CASE[::-1] | ||
|
|
||
|
|
||
| def insert_sort(l): | ||
| """Sort a list by inserting values in order after comparing each value.""" | ||
| if len(l) <= 1: | ||
| return | ||
| for idx in range(1, len(l)): | ||
| cur = l[idx] | ||
| spot = idx | ||
| while spot > 0 and l[spot - 1] > cur: | ||
| l[spot] = l[spot - 1] | ||
| spot = spot - 1 | ||
| l[spot] = cur | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| print("Best Case: a list already in order") | ||
| print("number of runs: 1000 / average time: " + str((timeit.Timer( | ||
| "insert_sort(BEST_CASE)", setup="from __main__ import insert_sort, BEST_CASE").timeit( | ||
| number=1000))/1000)) | ||
| print("Worst Case: a list in reverse order") | ||
| print("number of runs: 1000 / average time: " + str( | ||
| (timeit.Timer( | ||
| "insert_sort(WORST_CASE)", setup="from __main__ import insert_sort, WORST_CASE").timeit( | ||
| number=1000))/1000)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import unicode_literals | ||
| import copy | ||
| import random | ||
| import pytest | ||
|
|
||
|
|
||
| TEST_LIST = [ | ||
| [], [8] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
| ] | ||
|
|
||
| for x in range(0, 10): | ||
| y = (random.sample(range(100), random.randint(1, 50))) | ||
| TEST_LIST.append(y) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('test_list', TEST_LIST) | ||
| def test_insertion_sort_random(test_list): | ||
| from insert_sort import insert_sort | ||
| start_list = copy.copy(test_list) | ||
| insert_sort(test_list) | ||
| assert test_list == sorted(start_list) | ||
|
|
||
|
|
||
| def test_best_case(): | ||
| from insert_sort import BEST_CASE | ||
| assert isinstance(BEST_CASE, list) | ||
|
|
||
|
|
||
| def test_worst_case(): | ||
| from insert_sort import WORST_CASE | ||
| assert isinstance(WORST_CASE, list) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'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?