-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket_sort.py
92 lines (71 loc) · 3.16 KB
/
bucket_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from typing import List, Optional
from quick_sort import QuickSort
from radix_sort import RadixSort
class BucketSort:
"""
Implementation of bucket sort algorithm with a radix sort subroutine.
"""
@staticmethod
def sorter(arr: List[int], minimum: Optional[int] = None, maximum: Optional[int] = None) -> List[int]:
"""
Sorts an array in ascending order using the bucket sort algorithm.
Args:
arr (List[int]): The array of elements to be sorted.
minimum (Optional[int]): The minimum value in the array. If not provided, it will be calculated.
maximum (Optional[int]): The maximum value in the array. If not provided, it will be calculated.
Returns:
List[int]: The sorted array.
"""
if len(arr) == 0:
return arr
# Determine the range of the input array
min_value = minimum if minimum is not None else min(arr)
max_value = maximum if maximum is not None else max(arr)
bucket_range = (max_value - min_value) / len(arr) + 1 # bucket size
# Create buckets
buckets = [[] for _ in range(len(arr))]
# Distribute input array values into buckets
for num in arr:
index = int((num - min_value) / bucket_range)
# Ensure the index is within the correct range
index = min(index, len(arr) - 1)
buckets[index].append(num)
# Sort each bucket using insertion sort and concatenate the results
sorted_arr = []
for bucket in buckets:
sorted_arr.extend(RadixSort.sorter(bucket))
return sorted_arr
class QuickBucketSort:
"""
Implementation of bucket sort algorithm with a quicksort subroutine.
"""
@staticmethod
def sorter(arr: List[int], minimum: Optional[int] = None, maximum: Optional[int] = None) -> List[int]:
"""
Sorts an array in ascending order using the bucket sort algorithm.
Args:
arr (List[int]): The array of elements to be sorted.
minimum (Optional[int]): The minimum value in the array. If not provided, it will be calculated.
maximum (Optional[int]): The maximum value in the array. If not provided, it will be calculated.
Returns:
List[int]: The sorted array.
"""
if len(arr) == 0:
return arr
# Determine the range of the input array
min_value = minimum if minimum is not None else min(arr)
max_value = maximum if maximum is not None else max(arr)
bucket_range = (max_value - min_value) / len(arr) + 1 # bucket size
# Create buckets
buckets = [[] for _ in range(len(arr))]
# Distribute input array values into buckets
for num in arr:
index = int((num - min_value) / bucket_range)
# Ensure the index is within the correct range
index = min(index, len(arr) - 1)
buckets[index].append(num)
# Sort each bucket using insertion sort and concatenate the results
sorted_arr = []
for bucket in buckets:
sorted_arr.extend(QuickSort.sorter(bucket))
return sorted_arr