|
1 | 1 | """ General and special implementations of bucket sort. """
|
2 | 2 | __author__ = 'Claus Martinsen'
|
3 | 3 |
|
| 4 | +from insertion_sort import insertion_sort |
| 5 | + |
4 | 6 |
|
5 | 7 | def bucket_sort_0_1(lst):
|
6 | 8 | """
|
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 |
11 | 20 | """
|
12 | 21 | n = len(lst)
|
13 | 22 | buckets = [[] for _ in range(n)]
|
14 | 23 | for num in lst:
|
15 | 24 | buckets[int(n * num)].append(num)
|
16 | 25 |
|
17 |
| - sorted_list = [] #TODO: use the same list |
| 26 | + lst.clear() # Make the list ready for re-use |
18 | 27 | for bucket in buckets:
|
19 | 28 | 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 |
22 | 31 |
|
23 | 32 |
|
24 | 33 | def bucket_sort_general(lst, mapping):
|
25 | 34 | """
|
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'. |
27 | 52 |
|
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 |
31 | 60 | """
|
32 | 61 | n = len(lst)
|
33 | 62 | buckets = [[] for _ in range(n)]
|
34 | 63 | for num in lst:
|
35 | 64 | buckets[mapping(num) * n].append(num)
|
36 | 65 |
|
37 |
| - sorted_list = [] |
| 66 | + lst.clear() # Make the list ready for re-use |
38 | 67 | for bucket in buckets:
|
39 | 68 | 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 |
42 | 71 |
|
43 | 72 |
|
44 | 73 | if __name__ == '__main__':
|
|
0 commit comments