Skip to content
Closed
Changes from all commits
Commits
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
68 changes: 68 additions & 0 deletions sorts/flashsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
This is a Python implementation of the flashsort algorithm.

For doctests run the following command:
python3 -m doctest -v flashsort.py

For manual testing run:
python3 flashsort.py
"""


def flash_sort(collection: list) -> list:
"""A pure Python implementation of the flash sort algorithm.

:param collection: a mutable collection of comparable items in any order
:return: the same collection in ascending order

Examples:
>>> flash_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> flash_sort([])
[]
>>> flash_sort([-2, 5, 0, -45])
[-45, -2, 0, 5]
>>> collections = ([], [0, 5, 3, 2, 2], [-2, 5, 0, -45])
>>> all(sorted(collection) == flash_sort(collection) for collection in collections)
True
"""

n = len(collection)
if n < 2:
return collection

max_value = max(collection)
min_value = min(collection)

m = int(n / 2)
L = [0] * m

Check failure on line 38 in sorts/flashsort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

sorts/flashsort.py:38:5: N806 Variable `L` in function should be lowercase
if max_value == min_value:
return collection

for value in collection:
class_index = int(m * (value - min_value) / (max_value - min_value))
if class_index >= m:
class_index = m - 1
L[class_index] += 1

for i in range(1, m):
L[i] += L[i - 1]

output = [0] * n
for i in range(n - 1, -1, -1):
value = collection[i]
class_index = int(m * (value - min_value) / (max_value - min_value))
if class_index >= m:
class_index = m - 1
output[L[class_index] - 1] = value
L[class_index] -= 1
for i in range(n):
collection[i] = output[i]

return collection


if __name__ == "__main__":
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(flash_sort(unsorted))
Loading