Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 14f0304

Browse files
Bucket Sort
1 parent 0e7addb commit 14f0304

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ Data Structures and Algorithms Patterns implemented in Python.
2727
- [x] [Insertion Sort](Sorting-Algo/insertionsort.py)
2828
- [x] [Shell Sort](Sorting-Algo/shellsort.py)
2929
- [x] [Selection Sort](Sorting-Algo/selectionsort.py)
30+
- [x] [Bucket Sort](Sorting-Algo/bucketsort.py)
3031

‎Sorting-Algo/bucketsort.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Bucket Sort
3+
Bucket Sort is a sorting algorithm that divides the unsorted list elements into several groups called buckets. Each bucket is then sorted by using any of the suitable sorting algorithms or recursively applying the same bucket algorithm.
4+
5+
Finally, the sorted buckets are combined to form a final sorted list.
6+
7+
Average & Best Case Time Complexity: O(n+k)
8+
Worst Case Time Complexity: O(n*n)
9+
'''
10+
def bucketSort(list):
11+
bucket = []
12+
13+
# Create empty buckets
14+
for i in range(len(list)):
15+
bucket.append([])
16+
17+
# Insert elements into their respective buckets
18+
for j in list:
19+
index_b = int(10 * j)
20+
bucket[index_b].append(j)
21+
22+
# Sort the elements of each bucket
23+
for i in range(len(list)):
24+
bucket[i] = sorted(bucket[i])
25+
26+
# Get the sorted elements
27+
k = 0
28+
for i in range(len(list)):
29+
for j in range(len(bucket[i])):
30+
list[k] = bucket[i][j]
31+
k += 1
32+
return list
33+
34+
35+
list = [.42, .32, .33, .52, .37, .47, .51]
36+
print(bucketSort(list)) #[0.32, 0.33, 0.37, 0.42, 0.47, 0.51, 0.52]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /