- Notifications
You must be signed in to change notification settings - Fork25
Added counting sort (Issue #23)#48
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
n-gao wants to merge1 commit intoabranhe:masterChoose a base branch fromn-gao:master
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.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsallalgorithms/sorting/__init__.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
56 changes: 56 additions & 0 deletionsallalgorithms/sorting/counting_sort.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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # -*- coding: UTF-8 -*- | ||
| # | ||
| # Radix Sort Algorithm | ||
| # The All ▲lgorithms library for python | ||
| # | ||
| # Contributed by: Nicholas Gao | ||
| # Github: @n-gao | ||
| # | ||
| class BinaryTree: | ||
| def __init__(self, value): | ||
| self.value = value | ||
| self.left_child = None | ||
| self.right_child = None | ||
| def add_child(self, value): | ||
| if value < self.value: | ||
| if self.left_child is None: | ||
| self.left_child = BinaryTree(value) | ||
| else: | ||
| self.left_child.add_child(value) | ||
| else: | ||
| if self.right_child is None: | ||
| self.right_child = BinaryTree(value) | ||
| else: | ||
| self.right_child.add_child(value) | ||
| def get_sorted_values(self): | ||
| result = [] | ||
| if self.left_child is not None: | ||
| result = self.left_child.get_sorted_values() | ||
| result.append(self.value) | ||
| if self.right_child is not None: | ||
| result = result + self.right_child.get_sorted_values() | ||
| return result | ||
| def counting_sort(arr): | ||
| counts = {} | ||
| tree = None | ||
| for value in arr: | ||
| if value in counts: | ||
| counts[value] += 1 | ||
| else: | ||
| counts[value] = counts[value] + 1 if value in counts else 1 | ||
| if tree is None: | ||
| tree = BinaryTree(value) | ||
| else: | ||
| tree.add_child(value) | ||
| sorted_values = tree.get_sorted_values() | ||
| i = 0 | ||
| for value in sorted_values: | ||
| for _ in range(counts[value]): | ||
| arr[i] = value | ||
| i += 1 | ||
| return arr |
39 changes: 39 additions & 0 deletionsdocs/sorting/counting-sort.md
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,39 @@ | ||
| # Counting sort | ||
| Counting sort is a sorting algorithm which does not actually sort | ||
| the array but rather reconstructs it. It operates by counting how | ||
| often every unique item (e.g., a certain number) occurs, sorts the | ||
| unique items by TreeSort and then reconstructs an array by copying | ||
| every unique item (the number of occurences)th times into the result. | ||
| Its advantage is the fast computation which is only linear with respect | ||
| of the input array *if* the number of unique items is significantly | ||
| smaller than the number of items. | ||
| ## Install | ||
| ``` | ||
| pip install allalgorithms | ||
| ``` | ||
| ## Usage | ||
| ```py | ||
| from allalgorithms.sorting import counting_sort | ||
| arr = [77, 2, 10, -2, 1, 7] | ||
| print(counting_sort(arr)) | ||
| # -> [-2, 1, 2, 7, 10, 77] | ||
| ``` | ||
| ## API | ||
| ``` | ||
| counting_sort(array) | ||
| ``` | ||
| > Returns a sorted array (does in-place replacements) | ||
| ##### Params: | ||
| - `array`: Array to sort | ||
4 changes: 4 additions & 0 deletionstests/test_sorting.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
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.