-
Notifications
You must be signed in to change notification settings - Fork 0
/
BucketSort.py
31 lines (26 loc) · 891 Bytes
/
BucketSort.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
def sort(array, bucketSize=DEFAULT_BUCKET_SIZE):
if len(array) == 0:
return array
# Determine minimum and maximum values
minValue = array[0]
maxValue = array[0]
for i in range(1, len(array)):
if array[i] < minValue:
minValue = array[i]
elif array[i] > maxValue:
maxValue = array[i]
# Initialize buckets
bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
buckets = []
for i in range(0, bucketCount):
buckets.append([])
# Distribute input array values into buckets
for i in range(0, len(array)):
buckets[math.floor((array[i] - minValue) / bucketSize)].append(array[i])
# Sort buckets and place back into input array
array = []
for i in range(0, len(buckets)):
insertion_sort.sort(buckets[i])
for j in range(0, len(buckets[i])):
array.append(buckets[i][j])
return array