forked from hariom20singh/python-learning-codes
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucketSort.py
More file actions
Latest commit
44 lines (38 loc) · 852 Bytes
/
Copy pathbucketSort.py
File metadata and controls
44 lines (38 loc) · 852 Bytes
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
# Python3 program to sort an array
# using bucket sort
definsertionSort(b):
foriinrange(1, len(b)):
up=b[i]
j=i-1
whilej>=0andb[j] >up:
b[j+1] =b[j]
j-=1
b[j+1] =up
returnb
defbucketSort(x):
arr= []
slot_num=10# 10 means 10 slots, each
# slot's size is 0.1
foriinrange(slot_num):
arr.append([])
# Put array elements in different buckets
forjinx:
index_b=int(slot_num*j)
arr[index_b].append(j)
# Sort individual buckets
foriinrange(slot_num):
arr[i] =insertionSort(arr[i])
# concatenate the result
k=0
foriinrange(slot_num):
forjinrange(len(arr[i])):
x[k] =arr[i][j]
k+=1
returnx
# Driver Code
x= [0.897, 0.565, 0.656,
0.1234, 0.665, 0.3434]
print("Sorted Array is")
print(bucketSort(x))
# This code is contributed by
# Oneil Hsiao