-
-
Notifications
You must be signed in to change notification settings - Fork 50.7k
Add hilbert sort #8176
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
Closed
Closed
Add hilbert sort #8176
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,44 @@ | ||
| import numpy as np | ||
|
|
||
|
|
||
| def hilbert_sort(data: list) -> list: | ||
|
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. As there is no test file in this pull request nor any test function or class in the file |
||
| """ | ||
| Imagine you have a bunch of balls, each with a different number on it. To sort the balls, you first throw them up in the air. As each ball falls back down, it follows a unique path based on its number. | ||
| As the balls fall back down and land, they come to rest at a location in 2D space. | ||
| To sort the balls based on their 2D coordinates, you can imagine lining up the balls along a winding road (Hilbert curve) based on their location. The balls that are close together along the road are also close together in 2D space. | ||
| Finally, you can read off the sorted order of the balls by simply following the order in which they appear along the winding road. This is similar to reading a book by following the order of the words on the page. | ||
| In this analogy the balls would start in 1D space and be transposed into 2D space, the 3rd dimension is mostly analogy. | ||
| """ | ||
| data = np.array(data) | ||
|
|
||
| # Map each number in the input array to a point in 2D space using the Hilbert curve | ||
| # Step 1: Convert each number to binary using zfill to ensure each binary string is 16 bits long | ||
| binary_array = np.array([list(bin(n)[2:].zfill(16)) for n in data], dtype=int) | ||
|
|
||
| # Step 2: Generate the coordinates for each binary string using the Hilbert curve | ||
| coords_array = np.zeros((len(data), 32)) | ||
|
|
||
| # Step 2a: Compute the odd-indexed coordinates using the cumulative sum of the product of the binary values and powers of 2 | ||
| coords_array[:, 1::2] = np.cumsum( | ||
| (-1) ** binary_array[:, ::-1] * 2 ** np.arange(16), axis=1 | ||
| )[:, ::-1] | ||
|
|
||
| # Step 2b: Compute the even-indexed coordinates using a cumulative product of the binary values, reversed and shifted by 1 | ||
| # We reverse the binary values so that the most significant bit is at the end of the array, making it easier to compute the cumulative product | ||
| # We shift the array by 1 so that we don't include the value for the current index in the product | ||
| # We use np.cumprod to compute the cumulative product of the binary values, which are either -1 or 1 | ||
| # We reverse the result again and concatenate a column of zeros to obtain the even-indexed coordinates | ||
| coords_array[:, ::2] = np.concatenate( | ||
| ( | ||
| np.zeros((len(data), 1)), | ||
| np.cumprod((-1) ** binary_array[:, ::-1][:, :-1], axis=1)[:, ::-1], | ||
| ), | ||
| axis=1, | ||
| ) | ||
|
|
||
| # Step 2c: Compute the cumulative sum of the coordinates to obtain the final Hilbert curve coordinates | ||
| np.add.accumulate(coords_array, axis=1, out=coords_array) | ||
|
|
||
| # Step 3: Sort the data array based on the Hilbert curve coordinates | ||
| sorted_indices = np.lexsort(coords_array.T) | ||
| return data[sorted_indices].tolist()[::-1] | ||
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.
As there is no test file in this pull request nor any test function or class in the file
sorts/hilbert_sort.py, please provide doctest for the functionhilbert_sort