mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create bucket_sort.py
This commit is contained in:
parent
4d2e7857b2
commit
384b79adf8
30
sorting/bucket_sort.py
Normal file
30
sorting/bucket_sort.py
Normal file
@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
def bucket_sort(list):
|
||||
|
||||
buckets = [[] for _ in range(K)]
|
||||
|
||||
shift = min(lst)
|
||||
max_val = max(lst) - shift
|
||||
bucket_size = max(1, max_val / K)
|
||||
|
||||
for i, elem in enumerate(lst):
|
||||
|
||||
index = (elem - shift) // bucket_size
|
||||
|
||||
if index == K:
|
||||
buckets[K - 1].append(elem)
|
||||
else:
|
||||
buckets[index].append(elem)
|
||||
|
||||
for bucket in buckets:
|
||||
bucket.sort()
|
||||
|
||||
sorted_array = []
|
||||
for bucket in buckets:
|
||||
sorted_array.extend(bucket)
|
||||
|
||||
for i in range(len(lst)):
|
||||
lst[i] = sorted_array[i]
|
Loading…
x
Reference in New Issue
Block a user