-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbucket_sort.py
More file actions
42 lines (31 loc) · 954 Bytes
/
Copy pathbucket_sort.py
File metadata and controls
42 lines (31 loc) · 954 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
35
36
37
38
39
40
41
42
def insertion_sort(bucket):
for i in range(1, len(bucket)):
key = bucket[i]
j = i - 1
while j >= 0 and key < bucket[j]:
bucket[j + 1] = bucket[j]
j -= 1
bucket[j + 1] = key
def bucket_sort(arr):
if len(arr) == 0:
return arr
max_value = max(arr)
size = max_value / len(arr)
buckets = [[] for _ in range(len(arr))]
for i in range(len(arr)):
j = int(arr[i] / size)
if j != len(arr):
buckets[j].append(arr[i])
else:
buckets[len(arr) - 1].append(arr[i])
for bucket in buckets:
insertion_sort(bucket)
result = []
for bucket in buckets:
result.extend(bucket)
return result
if __name__ == "__main__":
data = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]
print("Unsorted Array:", data)
sorted_data = bucket_sort(data)
print("Sorted Array:", sorted_data)