Skip to content

Commit 8321fe7

Browse files
Merge pull request codemistic#475 from rohansrivastava5491/patch-2
Added bucket sort in Python
2 parents 144c737 + 27e07ed commit 8321fe7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# Bucket Sort in Python
3+
4+
5+
def bucketSort(array):
6+
bucket = []
7+
8+
# Create empty buckets
9+
for i in range(len(array)):
10+
bucket.append([])
11+
12+
# Insert elements into their respective buckets
13+
for j in array:
14+
index_b = int(10 * j)
15+
bucket[index_b].append(j)
16+
17+
# Sort the elements of each bucket
18+
for i in range(len(array)):
19+
bucket[i] = sorted(bucket[i])
20+
21+
# Get the sorted elements
22+
k = 0
23+
for i in range(len(array)):
24+
for j in range(len(bucket[i])):
25+
array[k] = bucket[i][j]
26+
k += 1
27+
return array
28+
29+
30+
array = [.42, .32, .33, .52, .37, .47, .51]
31+
print("Sorted Array in descending order is")
32+
print(bucketSort(array))

0 commit comments

Comments
 (0)