-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Add flash sort algorithm implementation #11970
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
Changes from 9 commits
476ccc3
aa24123
5dd34bd
e7073ae
5c08b2d
098147f
76aff03
23655a4
322398a
d6da2d6
e6e020e
4cb1146
5e8de60
4422644
6518ffa
50e3583
ab40ed1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
""" | ||
Implementation of Flash Sort in Python | ||
Author: Aman Gupta | ||
|
||
For doctests, run the following command: | ||
python3 -m doctest -v flash_sort.py | ||
|
||
For manual testing, run: | ||
python3 flash_sort.py | ||
""" | ||
|
||
from __future__ import annotations | ||
from collections.abc import Callable | ||
|
||
|
||
class FlashSort: | ||
def __init__(self, arr: list[int], n_classes: int, sort_key: Callable[[int], int] = lambda x: x): | ||
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. Please provide return type hint for the function: Please provide descriptive name for the parameter: 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. Please provide return type hint for the function: Please provide descriptive name for the parameter: 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. Please provide return type hint for the function: Please provide descriptive name for the parameter: |
||
self.arr = arr | ||
self.n = len(arr) | ||
self.n_classes = n_classes | ||
self.sort_key = sort_key | ||
|
||
def flash_sort(self) -> None: | ||
""" | ||
Performs flash sort on the array in-place. | ||
|
||
>>> arr = [5, 3, 8, 6, 2, 7, 4, 1] | ||
>>> sorter = FlashSort(arr, n_classes=5) | ||
>>> sorter.flash_sort() | ||
>>> arr | ||
[1, 2, 3, 4, 5, 6, 7, 8] | ||
|
||
>>> arr = [1] | ||
>>> sorter = FlashSort(arr, n_classes=1) | ||
>>> sorter.flash_sort() | ||
>>> arr | ||
[1] | ||
|
||
>>> arr = [2, 2, 2] | ||
>>> sorter = FlashSort(arr, n_classes=2) | ||
>>> sorter.flash_sort() | ||
>>> arr | ||
[2, 2, 2] | ||
""" | ||
if self.n <= 1: | ||
return | ||
|
||
min_val = min(self.arr, key=self.sort_key) | ||
max_val = max(self.arr, key=self.sort_key) | ||
|
||
if self.sort_key(min_val) == self.sort_key(max_val): | ||
return | ||
|
||
count = [0] * self.n_classes # Renamed variable `l` to `count` | ||
c1 = (self.n_classes - 1) / (self.sort_key(max_val) - self.sort_key(min_val)) | ||
|
||
# Classification step | ||
for i in range(self.n): | ||
k = int(c1 * (self.sort_key(self.arr[i]) - self.sort_key(min_val))) | ||
count[k] += 1 | ||
|
||
# Cumulative step | ||
for i in range(1, self.n_classes): | ||
count[i] += count[i - 1] | ||
|
||
# Permutation step | ||
i, nmove, flash = 0, 0, self.arr[0] | ||
while nmove < self.n: | ||
while i >= count[int(c1 * (self.sort_key(flash) - self.sort_key(min_val)))]: | ||
i += 1 | ||
flash = self.arr[i] | ||
k = int(c1 * (self.sort_key(flash) - self.sort_key(min_val))) | ||
|
||
while i != count[k]: | ||
k = int(c1 * (self.sort_key(flash) - self.sort_key(min_val))) | ||
hold = self.arr[count[k] - 1] | ||
self.arr[count[k] - 1] = flash | ||
flash = hold | ||
count[k] -= 1 | ||
nmove += 1 | ||
|
||
# Final step: Insertion sort to finish | ||
for i in range(1, self.n): | ||
key_item = self.arr[i] | ||
j = i - 1 | ||
while j >= 0 and self.arr[j] > key_item: | ||
self.arr[j + 1] = self.arr[j] | ||
j -= 1 | ||
self.arr[j + 1] = key_item | ||
|
||
|
||
if __name__ == "__main__": | ||
user_input = input("Enter numbers separated by a comma:\n").strip() | ||
if user_input == "": | ||
unsorted = [] | ||
else: | ||
unsorted = [int(item.strip()) for item in user_input.split(",")] | ||
sorter = FlashSort(unsorted, n_classes=5) | ||
sorter.flash_sort() | ||
print(unsorted) |
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.
Please provide return type hint for the function:
__init__
. If the function does not return a value, please provide the type hint as:def function() -> None:
Please provide descriptive name for the parameter:
x