File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ def insertionSort (b ):
2
+ for i in range (1 , len (b )):
3
+ up = b [i ]
4
+ j = i - 1
5
+ while j >= 0 and b [j ] > up :
6
+ b [j + 1 ] = b [j ]
7
+ j -= 1
8
+ b [j + 1 ] = up
9
+ return b
10
+
11
+ def bucketSort (x ):
12
+ arr = []
13
+ slot_num = 10 # 10 means 10 slots, each
14
+ # slot's size is 0.1
15
+ for i in range (slot_num ):
16
+ arr .append ([])
17
+
18
+ # Put array elements in different buckets
19
+ for j in x :
20
+ index_b = int (slot_num * j )
21
+ arr [index_b ].append (j )
22
+
23
+ # Sort individual buckets
24
+ for i in range (slot_num ):
25
+ arr [i ] = insertionSort (arr [i ])
26
+
27
+ # concatenate the result
28
+ k = 0
29
+ for i in range (slot_num ):
30
+ for j in range (len (arr [i ])):
31
+ x [k ] = arr [i ][j ]
32
+ k += 1
33
+ return x
34
+
35
+ # Driver Code
36
+ x = [0.897 , 0.565 , 0.656 ,
37
+ 0.1234 , 0.665 , 0.3434 ]
38
+ print ("Sorted Array is" )
39
+ print (bucketSort (x ))
You can’t perform that action at this time.
0 commit comments