-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbucket-sort.py
More file actions
30 lines (23 loc) · 727 Bytes
/
bucket-sort.py
File metadata and controls
30 lines (23 loc) · 727 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
# Python implementation of Bucket Sort Algorithm
def bucketSort(array):
bucket = []
# Create empty buckets
for i in range(len(array)):
bucket.append([])
# Insert elements into their respective buckets
for j in array:
index_b = int(10 * j)
bucket[index_b].append(j)
# Sort the elements of each bucket
for i in range(len(array)):
bucket[i] = sorted(bucket[i])
# Get the sorted elements
k = 0
for i in range(len(array)):
for j in range(len(bucket[i])):
array[k] = bucket[i][j]
k += 1
return array
array = [.42, .32, .33, .52, .37, .47, .51, .51]
print("Sorted Array in descending order is")
print(bucketSort(array))