Skip to content

Commit 8ef8252

Browse files
authored
Merge pull request #249 from priyanshi5/sort
Solved Bucket Sort
2 parents 2ccfd71 + 1cbe46e commit 8ef8252

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Sorting Algorithms/Bucket Sort.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def insertionSort(b):
2+
for i in range(1, len(b)):
3+
up = b[i]
4+
j = i - 1
5+
while j >= 0 and b[j] > up:
6+
b[j + 1] = b[j]
7+
j -= 1
8+
b[j + 1] = up
9+
return b
10+
11+
def bucketSort(x):
12+
arr = []
13+
slot_num = 10 # 10 means 10 slots, each
14+
# slot's size is 0.1
15+
for i in range(slot_num):
16+
arr.append([])
17+
18+
# Put array elements in different buckets
19+
for j in x:
20+
index_b = int(slot_num * j)
21+
arr[index_b].append(j)
22+
23+
# Sort individual buckets
24+
for i in range(slot_num):
25+
arr[i] = insertionSort(arr[i])
26+
27+
# concatenate the result
28+
k = 0
29+
for i in range(slot_num):
30+
for j in range(len(arr[i])):
31+
x[k] = arr[i][j]
32+
k += 1
33+
return x
34+
35+
# Driver Code
36+
x = [0.897, 0.565, 0.656,
37+
0.1234, 0.665, 0.3434]
38+
print("Sorted Array is")
39+
print(bucketSort(x))

0 commit comments

Comments
 (0)