-
-
Notifications
You must be signed in to change notification settings - Fork 48.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 1 commit
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,88 @@ | ||
def flash_sort(array: list[int]) -> list[int]: | ||
""" | ||
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) | ||
|
||
# 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] | ||
if array[i] < min_value: | ||
min_value = array[i] | ||
if min_value == max_value: | ||
return array # All values are the same | ||
|
||
# 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: | ||
yashleo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return (m * (value - min_value)) // (max_value - min_value + 1) | ||
|
||
Lb = [0] * m | ||
|
||
for value in array: | ||
Lb[get_bucket_id(value)] += 1 | ||
|
||
# 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: | ||
|
||
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): | ||
|
||
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]: | ||
|
||
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])) | ||
Uh oh!
There was an error while loading. Please reload this page.