Skip to content

Commit 3d41a37

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 3f9363e commit 3d41a37

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

sorts/flashsort.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
from typing import List
22

3+
34
def flashsort(arr: List[int]) -> List[int]:
45
"""
5-
Flashsort is a distribution sorting algorithm showing linear computational complexity O(n) for uniformly distributed data sets and relatively little additional memory requirement. The original work was published in 1998 by Karl-Dietrich Neubert.[1]
6-
7-
Concept
8-
Flashsort is an efficient in-place implementation of histogram sort, itself a type of bucket sort. It assigns each of the n input elements to one of m buckets, efficiently rearranges the input to place the buckets in the correct order, then sorts each bucket. The original algorithm sorts an input array A as follows:
9-
10-
Using a first pass over the input or a priori knowledge, find the minimum and maximum sort keys.
11-
Linearly divide the range [Amin, Amax] into m buckets.
12-
Make one pass over the input, counting the number of elements Ai which fall into each bucket. (Neubert calls the buckets "classes" and the assignment of elements to their buckets "classification".)
13-
Convert the counts of elements in each bucket to a prefix sum, where Lb is the number of elements Ai in bucket b or less. (L0 = 0 and Lm = n.)
14-
Rearrange the input so all elements of each bucket b are stored in positions Ai where Lb−1 < i ≤ Lb.
15-
Sort each bucket using insertion sort.
16-
Steps 1–3 and 6 are common to any bucket sort, and can be improved using techniques generic to bucket sorts. In particular, the goal is for the buckets to be of approximately equal size (n/m elements each),[1] with the ideal being division into m quantiles. While the basic algorithm is a linear interpolation sort, if the input distribution is known to be non-uniform, a non-linear division will more closely approximate this ideal. Likewise, the final sort can use any of a number of techniques, including a recursive flash sort.
17-
18-
What distinguishes flash sort is step 5: an efficient O(n) in-place algorithm for collecting the elements of each bucket together in the correct relative order using only m words of additional memory.
19-
References:
20-
- https://en.wikipedia.org/wiki/Flashsort
6+
Flashsort is a distribution sorting algorithm showing linear computational complexity O(n) for uniformly distributed data sets and relatively little additional memory requirement. The original work was published in 1998 by Karl-Dietrich Neubert.[1]
7+
8+
Concept
9+
Flashsort is an efficient in-place implementation of histogram sort, itself a type of bucket sort. It assigns each of the n input elements to one of m buckets, efficiently rearranges the input to place the buckets in the correct order, then sorts each bucket. The original algorithm sorts an input array A as follows:
10+
11+
Using a first pass over the input or a priori knowledge, find the minimum and maximum sort keys.
12+
Linearly divide the range [Amin, Amax] into m buckets.
13+
Make one pass over the input, counting the number of elements Ai which fall into each bucket. (Neubert calls the buckets "classes" and the assignment of elements to their buckets "classification".)
14+
Convert the counts of elements in each bucket to a prefix sum, where Lb is the number of elements Ai in bucket b or less. (L0 = 0 and Lm = n.)
15+
Rearrange the input so all elements of each bucket b are stored in positions Ai where Lb−1 < i ≤ Lb.
16+
Sort each bucket using insertion sort.
17+
Steps 1–3 and 6 are common to any bucket sort, and can be improved using techniques generic to bucket sorts. In particular, the goal is for the buckets to be of approximately equal size (n/m elements each),[1] with the ideal being division into m quantiles. While the basic algorithm is a linear interpolation sort, if the input distribution is known to be non-uniform, a non-linear division will more closely approximate this ideal. Likewise, the final sort can use any of a number of techniques, including a recursive flash sort.
18+
19+
What distinguishes flash sort is step 5: an efficient O(n) in-place algorithm for collecting the elements of each bucket together in the correct relative order using only m words of additional memory.
20+
References:
21+
- https://en.wikipedia.org/wiki/Flashsort
2122
"""
2223
if not isinstance(arr, list) or not all(isinstance(x, int) for x in arr):
2324
raise ValueError("Input must be a list of integers.")
@@ -55,6 +56,8 @@ def flashsort(arr: List[int]) -> List[int]:
5556

5657
return sorted_arr
5758

59+
5860
if __name__ == "__main__":
5961
import doctest
62+
6063
doctest.testmod()

0 commit comments

Comments
 (0)