-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbucketsort.py
More file actions
40 lines (32 loc) · 1.11 KB
/
bucketsort.py
File metadata and controls
40 lines (32 loc) · 1.11 KB
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
# coding: utf-8
from insertionsort import insertionsort
def bucketsort(lst):
""" Implementation of bucket sort algorithm """
if not lst:
return lst
# determine minimum and maximum values
vmin, vmax = min(lst), max(lst)
# the size of bucket
bucketsize = 5
# set the number of buckets
number = ((vmax - vmin) // bucketsize) + 1
# initialize buckets
buckets = [[] for _ in range(number)]
# distribute input array values into buckets
for val in iter(lst):
# determine the bucket for given value
ibucket = (val - vmin) // bucketsize
# put value for given bucket
buckets[ibucket].append(val)
nls = []
for i in range(len(buckets)):
# sort bucket use insertion sort algorithm
# it fast for small list sizes
buckets[i] = insertionsort(buckets[i])
# put sorted bucket back to list
nls.extend(buckets[i])
return nls
if __name__ in "__main__":
a = [1, 0, 2, 4, 5, 6, 2, 7, 9, 1, 3, 8, -1]
print('list :', a)
print('bucket sort :', bucketsort(a), bucketsort(a) == sorted(a))