Skip to content

Commit 0ed6aa1

Browse files
committed
added bucket sort
1 parent 5a7df75 commit 0ed6aa1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
3+
def bucketSort(array):
4+
largest = max(array)
5+
length = len(array)
6+
size = largest/length
7+
8+
# Create Buckets
9+
buckets = [[] for i in range(length)]
10+
11+
# Bucket Sorting
12+
for i in range(length):
13+
index = int(array[i]/size)
14+
if index != length:
15+
buckets[index].append(array[i])
16+
else:
17+
buckets[length - 1].append(array[i])
18+
19+
# Sorting Individual Buckets
20+
for i in range(len(array)):
21+
buckets[i] = sorted(buckets[i])
22+
23+
24+
# Flattening the Array
25+
result = []
26+
for i in range(length):
27+
result = result + buckets[i]
28+
29+
return result

0 commit comments

Comments
 (0)