-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bucket-Sort.py
46 lines (34 loc) · 1.04 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
46
import math
#For sorting values in Buckets
def insertionSort(arr):
for i in range(len(arr)):
key = arr[i]
j = i - 1
while j>=0 and arr[j]>key:
arr[j+1] = arr[j]
j = j - 1
arr[j+1] = key
return arr
def bucketSort(a):
n = len(a) # number of buckets
# Create n empty buckets where n is equal to the length of the input list
buckets_list= []
for i in range(n):
buckets_list.append([])
#inserting elements in their respective bucket
for i in range(len(a)):
bucket_index = int(n*a[i])
buckets_list[bucket_index].append(a[i])
# Sort individual buckets
for i in range(n):
buckets_list[i] = insertionSort(buckets_list[i])
k = 0
for i in range(n):
for j in range(len(buckets_list[i])):
a[k] = buckets_list[i][j]
k += 1
return a
# Driver Code
x = [0.1,0.545,0.543,0.2,0.25,0.30,0.9,0.2,0.2,0.3,0.4,0.5,0.56]
sorted_array = bucketSort(x)
print("Sorted Array is \n",sorted_array)