Skip to content

Commit 514f801

Browse files
authored
Merge pull request AllAlgorithms#24 from beingadityak/bucket_sort
Bucket sort Algorithm in Python
2 parents 11264ee + faa7988 commit 514f801

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

sorting/bucket_sort.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
3+
__author__ = "Aditya Krishnakumar"
4+
5+
6+
def bucket_sort(A):
7+
buckets = [[] for x in range(10)]
8+
for i, x in enumerate(A):
9+
buckets[int(x * len(buckets))].append(x)
10+
out = []
11+
for buck in buckets:
12+
out += isort(buck)
13+
return out
14+
15+
16+
def isort(A):
17+
if len(A) <= 1: return A
18+
i = 1
19+
while i < len(A):
20+
k = A[i]
21+
j = i - 1
22+
while j >= 0 and A[j] > k:
23+
A[j + 1] = A[j]
24+
A[j] = k
25+
j -= 1
26+
i += 1
27+
return A

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy