-
Notifications
You must be signed in to change notification settings - Fork 0
Q sort #18
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
jefimenko
wants to merge
12
commits into
m-sort
Choose a base branch
from
q-sort
base: m-sort
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
Q sort #18
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6d81693
Added quick sort method first try, still not functional.
0d08d30
Fixed general bugs. TODO: handle multiple occurences of values.
ffcb140
fixed q_sort to work and wrote a new test
henry808 b2ce06c
fixed q_sort to work on multiples of the same number and wrote a test…
henry808 984091a
Added a simple tes for repeated values.
d2875b6
Pulled fix for multiple values.
4585a39
Added demonstration of the worst.
b416237
redid the sort in a, i hope, clearer way
henry808 8ac5213
quick sort works and added a test for when the last two numbers are t…
henry808 90a6faa
added some tests and rewrote if statement to keep track of pivots
henry808 a05fa5c
merged
henry808 097ebd3
removed print statements
henry808 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from merge_sort import timed_func | ||
|
|
||
|
|
||
| def q_sort(sequence): | ||
| if len(sequence) <= 1: | ||
| # Return a sequence of one sorted item, or stop if no input | ||
| return sequence | ||
| if len(sequence) == 2: | ||
| # Return a sequence of two sorted items | ||
| if sequence[1] > sequence[0]: | ||
| return sequence | ||
| else: | ||
| return sequence[::-1] | ||
| pivot = get_pivot(sequence) | ||
| left = [] | ||
| right = [] | ||
| center = [] | ||
| for index, item in enumerate(sequence): | ||
| if item < pivot: | ||
| left.append(item) | ||
| elif item > pivot: | ||
| right.append(item) | ||
| else: | ||
| center.append(item) | ||
| return q_sort(left) + center + q_sort(right) | ||
|
|
||
|
|
||
| def get_pivot(sequence): | ||
| first = sequence[0] | ||
| last = sequence[-1] | ||
| mid_index = len(sequence) // 2 | ||
| middle = sequence[mid_index] | ||
|
|
||
| if last > first > middle or middle > first > last: | ||
| return first | ||
| if last > middle > first or first > middle > last: | ||
| return middle | ||
| return last | ||
|
|
||
|
|
||
|
|
||
| @timed_func | ||
| def timed_q_sort(sequence): | ||
| return q_sort(sequence) | ||
|
|
||
|
|
||
| def same_maker(how_many): | ||
| moAr = [] | ||
| for each in range(how_many): | ||
| moAr.append(1) | ||
| return moAr | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| inputs = [range(500), range(1000)] | ||
| inputs.append(same_maker(500)) | ||
| inputs.append(same_maker(997)) | ||
|
|
||
| for inp in inputs: | ||
| timed_q_sort(inp) |
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,59 @@ | ||
| from q_sort import get_pivot, q_sort | ||
|
|
||
|
|
||
| def test_get_pivot_f(): | ||
| a = [2, 1, 3] | ||
| assert get_pivot(a) == 2 | ||
| b = [2, 3, 1] | ||
| assert get_pivot(b) == 2 | ||
|
|
||
|
|
||
| def test_get_pivot_l(): | ||
| a = [1, 3, 2] | ||
| assert get_pivot(a) == 2 | ||
| b = [3, 1, 2] | ||
| assert get_pivot(b) == 2 | ||
|
|
||
|
|
||
| def test_get_pivot_m(): | ||
| a = [1, 5, 5, 2, 1, 1, 3] | ||
| assert get_pivot(a) == 2 | ||
| b = [3, 2, 1] | ||
| assert get_pivot(b) == 2 | ||
|
|
||
|
|
||
| def test_q_one(): | ||
| a = [4, 3, 2, 5, 7, 1, 6] | ||
| a = q_sort(a) | ||
| assert a == sorted(a) | ||
|
|
||
|
|
||
|
|
||
| def test_q_two(): | ||
| a = [5, 5, 5, 5, 5, 5, 5, 5, 5] | ||
| a = q_sort(a) | ||
| assert a == sorted(a) | ||
|
|
||
|
|
||
| def test_q_mixed(): | ||
| a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 10, 10] | ||
| a = q_sort(a) | ||
| assert a == sorted(a) | ||
|
|
||
|
|
||
| def test_q_mixed_duplicate_last_values_greater(): | ||
| a = [1, 2, 10, 10, 10] | ||
| a = q_sort(a) | ||
| assert a == sorted(a) | ||
|
|
||
|
|
||
| def test_q_mixed_duplicate_last_values_lesser(): | ||
| a = [1, 2, 5, 0, 0] | ||
| a = q_sort(a) | ||
| assert a == sorted(a) | ||
|
|
||
|
|
||
| def test_q_mixed(): | ||
| a = [1, 2, 5, 4, 4, 6, 7, 9, 8, 0, 0] | ||
| a = q_sort(a) | ||
| assert a == [0, 0, 1, 2, 4, 4, 5, 6, 7, 8, 9] | ||
|
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. given the tests you have, I feel assuaged that you aren't accidentally dropping any values while you are picking the pivot. |
||
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.
since your sort is not an in-place operation, you could short-circuit this: