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 2 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 @@
def flash_sort(array):

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)
n = len(array)

# Step 1: Find mininum 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 8 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

sorts/flash_sort.py:7: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 10 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

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

# Step 2: Divide array into m buckets
import math
m = max(math.floor(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

"""
Get the bucket index for the given value.

>>> get_bucket_id(5)
1
>>> get_bucket_id(10)
2
>>> get_bucket_id(1)
0
"""
return math.floor((m * (value - min_value)) / (max_value - min_value + 1))

bucket_counts = [0] * m
for value in array:
bucket_counts[get_bucket_id(value)] += 1

# Step 4: Convert the count to prefix sum
for i in range(1, m):
bucket_counts[i] = bucket_counts[i - 1] + bucket_counts[i]

# Step 5: Rearrange the elements
def find_swap_index(bucket_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

"""
Find the index for the first element in the given bucket.

>>> array = [15, 13, 24, 7, 18, 3, 22, 9]
>>> find_swap_index(1) # Modify array as needed before calling
1 # Update this return value based on the current state of the array
"""
for ind in range(bucket_counts[bucket_id - 1], bucket_counts[bucket_id]):
if get_bucket_id(array[ind]) != bucket_id:
break
return ind

def arrange_bucket(i1: int, i2: int, bucket_id: int) -> None:

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

"""
Arrange elements into the specified bucket.

>>> array = [15, 13, 24, 7, 18, 3, 22, 9]
>>> arrange_bucket(0, 4, 1) # Modify array as needed before calling
>>> array
[3, 13, 15, 7, 18, 24, 22, 9] # Update this expected result based on your implementation

Check failure on line 61 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

sorts/flash_sort.py:61:89: E501 Line too long (97 > 88)
"""
for i in range(i1, i2):
current_bucket_id = get_bucket_id(array[i])
while current_bucket_id != bucket_id:
swap_index = find_swap_index(current_bucket_id)
array[i], array[swap_index] = array[swap_index], array[i]
current_bucket_id = get_bucket_id(array[i])
return

Check failure on line 69 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1711)

sorts/flash_sort.py:69:9: PLR1711 Useless `return` statement at end of function

for bucket_id in range(0, m - 1):

Check failure on line 71 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PIE808)

sorts/flash_sort.py:71:28: PIE808 Unnecessary `start` argument in `range`
if bucket_id == 0:
arrange_bucket(bucket_id, bucket_counts[bucket_id], bucket_id)
else:
arrange_bucket(bucket_counts[bucket_id - 1], bucket_counts[bucket_id], bucket_id)

Check failure on line 75 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

sorts/flash_sort.py:75:89: E501 Line too long (93 > 88)

# Step 6: Sort each bucket
def insertion_sort(array) -> None:
"""
Sort the array using insertion sort.

>>> insertion_sort([4, 3, 2, 1])
>>> array
[1, 2, 3, 4]
"""
for i in range(1, len(array)):
temp = array[i]
j = i - 1
while j >= 0 and temp < array[j]:
array[j + 1] = array[j]
j -= 1
array[j + 1] = temp
return

Check failure on line 93 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1711)

sorts/flash_sort.py:93:9: PLR1711 Useless `return` statement at end of function

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

Check failure on line 99 in sorts/flash_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

sorts/flash_sort.py:99:89: E501 Line too long (119 > 88)
return
Loading