-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucketsort.py
36 lines (28 loc) · 1.09 KB
/
bucketsort.py
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
'''
Bucket Sort
Bucket Sort is a sorting algorithm that divides the unsorted list elements into several groups called buckets. Each bucket is then sorted by using any of the suitable sorting algorithms or recursively applying the same bucket algorithm.
Finally, the sorted buckets are combined to form a final sorted list.
Average & Best Case Time Complexity: O(n+k)
Worst Case Time Complexity: O(n*n)
'''
def bucketSort(list):
bucket = []
# Create empty buckets
for i in range(len(list)):
bucket.append([])
# Insert elements into their respective buckets
for j in list:
index_b = int(10 * j)
bucket[index_b].append(j)
# Sort the elements of each bucket
for i in range(len(list)):
bucket[i] = sorted(bucket[i])
# Get the sorted elements
k = 0
for i in range(len(list)):
for j in range(len(bucket[i])):
list[k] = bucket[i][j]
k += 1
return list
list = [.42, .32, .33, .52, .37, .47, .51]
print(bucketSort(list)) #[0.32, 0.33, 0.37, 0.42, 0.47, 0.51, 0.52]