Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 384b79a

Browse files
Create bucket_sort.py
1 parent 4d2e785 commit 384b79a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

sorting/bucket_sort.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# author: bt3gl
4+
5+
def bucket_sort(list):
6+
7+
buckets = [[] for _ in range(K)]
8+
9+
shift = min(lst)
10+
max_val = max(lst) - shift
11+
bucket_size = max(1, max_val / K)
12+
13+
for i, elem in enumerate(lst):
14+
15+
index = (elem - shift) // bucket_size
16+
17+
if index == K:
18+
buckets[K - 1].append(elem)
19+
else:
20+
buckets[index].append(elem)
21+
22+
for bucket in buckets:
23+
bucket.sort()
24+
25+
sorted_array = []
26+
for bucket in buckets:
27+
sorted_array.extend(bucket)
28+
29+
for i in range(len(lst)):
30+
lst[i] = sorted_array[i]

0 commit comments

Comments
 (0)