Skip to content

Commit e9f689a

Browse files
authored
Updated docstrings and re-used the lists
1 parent f76433b commit e9f689a

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

sorting/bucket_sort.py

+43-14
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,73 @@
11
""" General and special implementations of bucket sort. """
22
__author__ = 'Claus Martinsen'
33

4+
from insertion_sort import insertion_sort
5+
46

57
def bucket_sort_0_1(lst):
68
"""
7-
TBC
8-
9-
:param lst: An unsorted list of numbers between 0 and 1
10-
:return:
9+
Sorts a list of n floating point numbers between 0 and 1 by placing them
10+
into one of n buckets based on their value.
11+
12+
Each bucket is then sorted by using insertion sort (used because of its low
13+
overhead), but any sorting algorithm would do. The sorted buckets are then
14+
concatenated to one sorted list.
15+
16+
:param lst: An unsorted list of numbers between 0 and 1.
17+
:type lst: list
18+
:return: The list sorted in increasing order.
19+
:rtype: list
1120
"""
1221
n = len(lst)
1322
buckets = [[] for _ in range(n)]
1423
for num in lst:
1524
buckets[int(n * num)].append(num)
1625

17-
sorted_list = [] #TODO: use the same list
26+
lst.clear() # Make the list ready for re-use
1827
for bucket in buckets:
1928
if bucket: # Do not sort empty lists
20-
sorted_list += insertion_sort(bucket) # Add the sorted bucket to the sorted list
21-
return sorted_list
29+
lst += insertion_sort(bucket) # Add the sorted bucket to the sorted list
30+
return lst
2231

2332

2433
def bucket_sort_general(lst, mapping):
2534
"""
26-
TBC
35+
Sorts a list of n compareable elements by placing them into one of n
36+
buckets based on their value. Each element is mapped to a value between 0
37+
and 1 based on how big they are by the *mapping* parameter.
38+
39+
IMPORTANT: The mapping should be reflect the distribution of elements, so
40+
that each bucket has approximately the same probability of being filled.
41+
Example: A uniform distribution should have a uniform mapping function,
42+
and a normal distribution should have a normalized mapping.
43+
44+
If each bucket does not have the same probability of being filled, the
45+
average linear running-time will not be certain.
46+
47+
Each bucket is then sorted by using insertion sort (used because of its low
48+
overhead), but any sorting algorithm would do. The sorted buckets are then
49+
concatenated to one sorted list.
50+
51+
Also known as 'proxmap sort'.
2752
28-
:param lst: An unsorted list of items that...
29-
:param mapping:
30-
:return:
53+
:param lst: An unsorted list of compareable elements.
54+
:type lst: list
55+
:param mapping: A function that takes in an element and outputs a number
56+
between 0 and 1 indicating how big the number is.
57+
:type mapping: Callable
58+
:return: The list sorted in increasing order.
59+
:rtype: list
3160
"""
3261
n = len(lst)
3362
buckets = [[] for _ in range(n)]
3463
for num in lst:
3564
buckets[mapping(num) * n].append(num)
3665

37-
sorted_list = []
66+
lst.clear() # Make the list ready for re-use
3867
for bucket in buckets:
3968
if bucket: # Do not sort empty lists
40-
sorted_list += insertion_sort(bucket) # Add the sorted bucket to the sorted list
41-
return sorted_list
69+
lst += insertion_sort(bucket) # Add the sorted bucket to the sorted list
70+
return lst
4271

4372

4473
if __name__ == '__main__':

0 commit comments

Comments
 (0)