- Notifications
You must be signed in to change notification settings - Fork25
Added Heap sort, tests, and documentation.#36
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
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
48 changes: 48 additions & 0 deletionsallalgorithms/sorting/heap_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,48 @@ | ||
| # -*- coding: UTF-8 -*- | ||
| # | ||
| # Heap Sort Algorithm | ||
| # The All ▲lgorithms library for python | ||
| # | ||
| # Contributed by: DatHydroGuy | ||
| # Github: @DatHydroGuy | ||
| # | ||
| def build_heap(array_to_sort, array_length, index): | ||
| """ | ||
| Build a heap, where each node has two child nodes, and a root node is greater than both child nodes. | ||
| """ | ||
| largest = index # Flag the largest element as the last (root) element | ||
| left = 2 * index + 1 # Calculate index of left child node | ||
| right = 2 * index + 2 # Calculate index of right child node | ||
| # See if left child of root exists and is greater than root | ||
| if left < array_length and array_to_sort[index] < array_to_sort[left]: | ||
| largest = left | ||
| # See if right child of root exists and is greater than root | ||
| if right < array_length and array_to_sort[largest] < array_to_sort[right]: | ||
| largest = right | ||
| # If a larger element than root was found, swap with root so that root holds the new largest value | ||
| if largest != index: | ||
| array_to_sort[index], array_to_sort[largest] = array_to_sort[largest], array_to_sort[index] # swap | ||
| # Re-build the heap under the new largest root node | ||
| build_heap(array_to_sort, array_length, largest) | ||
| def heap_sort(array_to_sort): | ||
| """ | ||
| Builds a max-heap, then continuously removes the largest element and re-builds the heap until sorted | ||
| """ | ||
| array_length = len(array_to_sort) | ||
| # Build a max-heap to sort the elements into order | ||
| for index in range(array_length // 2 - 1, -1, -1): | ||
| build_heap(array_to_sort, array_length, index) | ||
| # One by one extract elements | ||
| for index in range(array_length - 1, 0, -1): | ||
| array_to_sort[index], array_to_sort[0] = array_to_sort[0], array_to_sort[index] # swap | ||
| build_heap(array_to_sort, index, 0) |
33 changes: 33 additions & 0 deletionsdocs/sorting/heap-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,33 @@ | ||
| # Heap Sort | ||
| Heap sort is a comparison-based sorting algorithm which operates on a Binary Heap data structure. It can be regarded as a version of Selection sort which is improved by use of the heap data structure rather than a linear-time search. | ||
| Heap sort is typically slower than Quicksort, but it does have a better worst-case scenario of O(n log n). It is an in-place algorithm, but does not produce a stable sort. | ||
| ## Install | ||
| ``` | ||
| pip install allalgorithms | ||
| ``` | ||
| ## Usage | ||
| ```py | ||
| from allalgorithms.sorting import heap_sort | ||
| arr = [77, 2, 10, -2, 1, 7] | ||
| heap_sort(arr) | ||
| print(arr) | ||
| # -> [-2, 1, 2, 7, 10, 77] | ||
| ``` | ||
| ## API | ||
| ### heap_sort(array) | ||
| > Performs an in-place sort of the passed-in array | ||
| ##### Params: | ||
| - `array`: Unsorted Array |
10 changes: 8 additions & 2 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.