File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Bucket Sort Algorithm
4
+ # The All ▲lgorithms library for python
5
+ #
6
+ # Contributed by: Dwij Sukeshkumar Sheth
7
+ # Github: @dwij2812
8
+ #
9
+
10
+ # Python3 program to sort an array
11
+ # using bucket sort
12
+ def insertionSort (b ):
13
+ for i in range (1 , len (b )):
14
+ up = b [i ]
15
+ j = i - 1
16
+ while j >= 0 and b [j ] > up :
17
+ b [j + 1 ] = b [j ]
18
+ j -= 1
19
+ b [j + 1 ] = up
20
+ return b
21
+
22
+ def bucketSort (x ):
23
+ arr = []
24
+ slot_num = 10 # 10 means 10 slots, each
25
+ # slot's size is 0.1
26
+ for i in range (slot_num ):
27
+ arr .append ([])
28
+
29
+ # Put array elements in different buckets
30
+ for j in x :
31
+ index_b = int (slot_num * j )
32
+ arr [index_b ].append (j )
33
+
34
+ # Sort individual buckets
35
+ for i in range (slot_num ):
36
+ arr [i ] = insertionSort (arr [i ])
37
+
38
+ # concatenate the result
39
+ k = 0
40
+ for i in range (slot_num ):
41
+ for j in range (len (arr [i ])):
42
+ x [k ] = arr [i ][j ]
43
+ k += 1
44
+ return x
45
+
46
+ # For using this make the function call bucketSort(data) where data is the array of the numbers to be sorted.
You can’t perform that action at this time.
0 commit comments