-
Notifications
You must be signed in to change notification settings - Fork 28
/
bucket-sort.py
45 lines (39 loc) · 1.28 KB
/
bucket-sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Bucket sort is a comparison sort algorithm that operates on elements by dividing them
into different buckets and then sorting these buckets individually. Each bucket is
sorted individually using a separate sorting algorithm or by applying the bucket
sort algorithm recursively. Bucket sort is mainly useful when the input is uniformly
distributed over a range.
Average & Best Case Time Complexity: O(n+k)
Worst Case Time Complexity: O(n*n)
Space complexity: O(n+k).
"""
import math
def insertionSort(arr):
for i in range(1, len(arr)):
temp = arr[i]
j = i - 1
while j >=0 and arr[j] > temp:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = temp
return arr
def bucketSort(arr):
buckets = [[] for _ in range(len(arr))] # Creating Buckets
divider = math.ceil((max(max(arr), len(arr))+1)/len(buckets))
# Adding numbers in buckets
for i in arr:
j = i//divider
buckets[j].append(i)
# Sorting the buckers
for i in range(len(buckets)):
buckets[i] = insertionSort(buckets[i])
# Merging Buckets
k = 0
for i in range(len(buckets)):
for j in range(len(buckets[i])):
arr[k] = buckets[i][j]
k += 1
arr = [10,9,8,7]
bucketSort(arr)
print(arr)