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 1 commit
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
88 changes: 88 additions & 0 deletions sorts/flash_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
def flash_sort(array: list[int]) -> list[int]:

Check failure on line 1 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (C901)

sorts/flash_sort.py:1:5: C901 `flash_sort` is too complex (21 > 17)
"""
Perform Flashsort on the given array.

Flashsort is a distribution-based sorting algorithm. It divides the array
into buckets based on value ranges and sorts within each bucket.

Arguments:
array -- list of integers to sort

Returns:
Sorted list of integers.

Example:
>>> flash_sort([15, 13, 24, 7, 18, 3, 22, 9])
[3, 7, 9, 13, 15, 18, 22, 24]
>>> flash_sort([5, 1, 4, 2, 3])
[1, 2, 3, 4, 5]
"""
n = len(array)

Check failure on line 21 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/flash_sort.py:21:1: W293 Blank line contains whitespace
# Step 1: Find minimum and maximum values
min_value, max_value = array[0], array[0]
for i in range(1, n):
if array[i] > max_value:
max_value = array[i]

Check failure on line 26 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

sorts/flash_sort.py:25:9: PLR1730 Replace `if` statement with `max_value = max(array[i], max_value)`
if array[i] < min_value:
min_value = array[i]

Check failure on line 28 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

sorts/flash_sort.py:27:9: PLR1730 Replace `if` statement with `min_value = min(array[i], min_value)`
if min_value == max_value:
return array # All values are the same

Check failure on line 31 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/flash_sort.py:31:1: W293 Blank line contains whitespace
# Step 2: Divide array into m buckets
m = max(int(0.45 * n), 1)

# Step 3: Count the number of elements in each class
def get_bucket_id(value: int) -> int:

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/flash_sort.py, please provide doctest for the function get_bucket_id

return (m * (value - min_value)) // (max_value - min_value + 1)

Check failure on line 38 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/flash_sort.py:38:1: W293 Blank line contains whitespace
Lb = [0] * m

Check failure on line 39 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

sorts/flash_sort.py:39:5: N806 Variable `Lb` in function should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: Lb

for value in array:
Lb[get_bucket_id(value)] += 1

Check failure on line 42 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

sorts/flash_sort.py:42:1: W293 Blank line contains whitespace
# Step 4: Convert the count to prefix sum
for i in range(1, m):
Lb[i] += Lb[i - 1]

# Step 5: Rearrange the elements
def find_swap_index(b_id: int) -> int:

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/flash_sort.py, please provide doctest for the function find_swap_index

for ind in range(Lb[b_id - 1], Lb[b_id]):
if get_bucket_id(array[ind]) != b_id:
break
return ind

def arrange_bucket(i1: int, i2: int, b: int):

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/flash_sort.py, please provide doctest for the function arrange_bucket

Please provide return type hint for the function: arrange_bucket. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: b

for i in range(i1, i2):
b_id = get_bucket_id(array[i])
while b_id != b:
s_ind = find_swap_index(b_id)
array[i], array[s_ind] = array[s_ind], array[i]
b_id = get_bucket_id(array[i])

for b in range(m - 1):
if b == 0:
arrange_bucket(b, Lb[b], b)
else:
arrange_bucket(Lb[b - 1], Lb[b], b)

# Step 6: Sort each bucket using insertion sort
def insertion_sort(sub_array: list[int]) -> list[int]:

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/flash_sort.py, please provide doctest for the function insertion_sort

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/flash_sort.py, please provide doctest for the function insertion_sort

for i in range(1, len(sub_array)):
temp = sub_array[i]
j = i - 1
while j >= 0 and temp < sub_array[j]:
sub_array[j + 1] = sub_array[j]
j -= 1
sub_array[j + 1] = temp
return sub_array

for i in range(m):
if i == 0:
array[i:Lb[i]] = insertion_sort(array[i:Lb[i]])
else:
array[Lb[i - 1]:Lb[i]] = insertion_sort(array[Lb[i - 1]:Lb[i]])

return array


# print(flash_sort([15, 13, 24, 7, 18, 3, 22, 9]))

Check failure on line 88 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

sorts/flash_sort.py:88:51: W292 No newline at end of file
Loading