-
Notifications
You must be signed in to change notification settings - Fork 35
refactor(3_merge_sort): Refactor merge_sort #11
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
LesterKim
wants to merge
4
commits into
ByteAcademyCo:master
Choose a base branch
from
LesterKim:refactor-mergesort
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5837161
refactor(3_merge_sort): Refactor merge_sort
LesterKim 16089b3
refactor(3_merge_sort): Use append instead of merged_list_index
LesterKim 9046dc9
fix(3_merge_sort): Remove unused integer variable
LesterKim 389d406
style(3_merge_sort): Update variables
LesterKim 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
62 changes: 30 additions & 32 deletions
62
...oduction_to_python/recursive_functions/3_merge_sort/solutions/.model_solution/solution.py
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 |
|---|---|---|
| @@ -1,32 +1,30 @@ | ||
| def merge_sort(num_list): | ||
| if len(num_list)>1: | ||
| mid = int(len(num_list)/2) | ||
| lefthalf = num_list[:mid] | ||
| righthalf = num_list[mid:] | ||
|
|
||
| merge_sort(lefthalf) | ||
| merge_sort(righthalf) | ||
|
|
||
| i=0 | ||
| j=0 | ||
| k=0 | ||
|
|
||
| while i < len(lefthalf) and j < len(righthalf): | ||
| if lefthalf[i] < righthalf[j]: | ||
| num_list[k]=lefthalf[i] | ||
| i=i+1 | ||
| else: | ||
| num_list[k]=righthalf[j] | ||
| j=j+1 | ||
| k=k+1 | ||
|
|
||
| while i < len(lefthalf): | ||
| num_list[k]=lefthalf[i] | ||
| i=i+1 | ||
| k=k+1 | ||
|
|
||
| while j < len(righthalf): | ||
| num_list[k]=righthalf[j] | ||
| j=j+1 | ||
| k=k+1 | ||
| return num_list | ||
| def merge_sort(numbers: list) -> list: | ||
| if len(numbers) < 2: | ||
| return numbers | ||
|
|
||
| middle_index = len(numbers) // 2 | ||
| left_half = numbers[:middle_index] | ||
| right_half = numbers[middle_index:] | ||
|
|
||
| return _merge(merge_sort(left_half), merge_sort(right_half)) | ||
|
|
||
|
|
||
| def _merge(left: list, right: list) -> list: | ||
| merged_list = [] | ||
| left_index = right_index = 0 | ||
|
|
||
| while left_index < len(left) and right_index < len(right): | ||
| if left[left_index] < right[right_index]: | ||
| merged_list.append(left[left_index]) | ||
| left_index += 1 | ||
| else: | ||
| merged_list.append(right[right_index]) | ||
| right_index += 1 | ||
|
|
||
| for index, numbers in ((left_index, left), (right_index, right)): | ||
| while index < len(numbers): | ||
| merged_list.append(numbers[index]) | ||
| index += 1 | ||
|
|
||
| return merged_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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Once students learn about Python
classes, they can create a non-publicclass _ListMergerthat contains this function as its public method.