-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket_sort.py
More file actions
34 lines (29 loc) · 817 Bytes
/
bucket_sort.py
File metadata and controls
34 lines (29 loc) · 817 Bytes
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
# -*- coding: utf-8 -*-
"""
@project: Sorting-Algorithms
@version: v1.0.0
@file: bucket_sort.py
@brief: Bucket Sort
@software: PyCharm
@author: Kai Sun
@email: autosunkai@gmail.com
@date: 2021/8/5 14:15
@updated: 2021/8/5 14:15
"""
def bucket_sort(arr):
min_val = min(arr)
max_val = max(arr)
res = []
# 桶的初始化
bucket_size = 5 # 桶的大小
bucket_count = (max_val - min_val) // bucket_size + 1 # 桶的数量
buckets = [[] for _ in range(bucket_count)]
# 利用映射函数将数据分配到每个桶中
for a in arr:
i = (a - min_val) // bucket_size
buckets[i].append(a)
for bucket in buckets:
bucket.sort()
for bucket in buckets:
res.extend(bucket) # 从 list 中提取
return res