Skip to content

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

Closed
wants to merge 17 commits into from
Closed
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions sorts/flash_sort.py
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:

Check failure on line 16 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

sorts/flash_sort.py:12:1: I001 Import block is un-sorted or un-formatted
def __init__(self, arr: list[int], n_classes: int, sort_key: Callable[[int], int] = lambda x: x):

Check failure on line 17 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

sorts/flash_sort.py:17:89: E501 Line too long (101 > 88)

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

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

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

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

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)
Loading