-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Kirkpatrcik Reisch Sort #11552
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
Oreldm
wants to merge
10
commits into
TheAlgorithms:master
Choose a base branch
from
Oreldm:kirkpatrcik_reisch_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
Kirkpatrcik Reisch Sort #11552
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ca76ce6
Kirkpatrcik Reisch Sort
Oreldm 431cb38
Added doctest
Oreldm ec3132e
Added Explanation Links
Oreldm 4205db1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 980d2a3
fix ruff checks
Oreldm 6e0e011
Merge branch 'kirkpatrcik_reisch_sort' of https://github.com/Oreldm/P…
Oreldm 838541d
PR Fix
Oreldm 490d710
PR Fix
Oreldm 39ae87f
PR Fix
Oreldm 4974bea
PR Fix
Oreldm 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,86 @@ | ||
import heapq | ||
import random | ||
from typing import List | ||
|
||
""" | ||
Kirkpatrick-Reisch sorting algorithm. | ||
Divides input into sqrt(n) blocks, sorts each, then merges using a min-heap. | ||
|
||
Time Complexity: | ||
- Average case: O(n * sqrt(n)) | ||
- Worst case: O(n * sqrt(n)) | ||
- Best case: O(n * sqrt(n)) | ||
|
||
Space Complexity: O(n) | ||
|
||
Explanation Links: | ||
https://en.wikipedia.org/wiki/Kirkpatrick%E2%80%93Reisch_sort | ||
https://sortingsearching.com/2020/06/06/kirkpatrick-reisch.html | ||
""" | ||
|
||
|
||
def kirkpatrick_reisch_sort(arr) -> List[int]: | ||
""" | ||
Implements the Kirkpatrick-Reisch sorting algorithm. | ||
|
||
Args: | ||
arr (list): The input list to be sorted. | ||
|
||
Returns: | ||
list: A new list containing the sorted elements. | ||
|
||
Examples: | ||
>>> kirkpatrick_reisch_sort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) | ||
[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9] | ||
|
||
>>> kirkpatrick_reisch_sort([]) | ||
[] | ||
|
||
>>> kirkpatrick_reisch_sort([1]) | ||
[1] | ||
|
||
>>> kirkpatrick_reisch_sort([5, 4, 3, 2, 1]) | ||
[1, 2, 3, 4, 5] | ||
|
||
>>> kirkpatrick_reisch_sort([-1, -3, 5, 0, 2]) | ||
[-3, -1, 0, 2, 5] | ||
""" | ||
n = len(arr) | ||
if n <= 1: | ||
return arr | ||
|
||
# Step 1: Divide the input into sqrt(n) blocks | ||
block_size = int(n**0.5) | ||
blocks = [arr[i : i + block_size] for i in range(0, n, block_size)] | ||
|
||
# Step 2: Sort each block | ||
for block in blocks: | ||
block.sort() | ||
|
||
# Step 3: Create a min-heap of the first elements of each block | ||
heap = [(block[0], i, 0) for i, block in enumerate(blocks) if block] | ||
heapq.heapify(heap) | ||
|
||
# Step 4: Extract elements from the heap and refill from blocks | ||
sorted_arr = [] | ||
while heap: | ||
val, block_index, element_index = heapq.heappop(heap) | ||
sorted_arr.append(val) | ||
|
||
if element_index + 1 < len(blocks[block_index]): | ||
next_element = blocks[block_index][element_index + 1] | ||
heapq.heappush(heap, (next_element, block_index, element_index + 1)) | ||
|
||
return sorted_arr | ||
|
||
|
||
if __name__ == "__main__": | ||
# Generate a random list of integers | ||
arr = [random.randint(1, 1000) for _ in range(100)] | ||
|
||
print("Original Array:", arr) | ||
sorted_arr = kirkpatrick_reisch_sort(arr) | ||
print("Sorted Array:", sorted_arr) | ||
|
||
# Verify the result | ||
assert sorted_arr == sorted(arr) |
Oops, something went wrong.
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.