File tree 1 file changed +38
-0
lines changed
Arrays-Sorting/src/Bucket_Sort
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Bucket sort, or bin sort, is a sorting algorithm that works by distributing
3
+ the elements of an array into a number of buckets. Each bucket is then sorted
4
+ individually, either using a different sorting algorithm, or by recursively
5
+ applying the bucket sorting algorithm.
6
+
7
+ https://en.wikipedia.org/wiki/Bucket_sort
8
+
9
+ """
10
+
11
+ from random import randint
12
+
13
+ def Bucket_Sort (A , Digit ):
14
+ B = [[] for x in range (10 )] #Creating 10 Buckets [10 different lists at every position]
15
+
16
+ #Storing elements in Bucket according to their first digit
17
+ for each in A :
18
+ B [int (str (each ).rjust (Digit , '0' )[0 ])].append (each )
19
+
20
+ #Sorting Buckets
21
+ for each in range (10 ):B [each ].sort ()
22
+
23
+ A = []
24
+
25
+ #Assembling elements from all the buckets
26
+ for bucket in B :
27
+ for each in bucket :
28
+ A .append (each )
29
+
30
+ return A
31
+
32
+ A = [randint (0 ,99 ) for x in range (15 )] #Generate a random list
33
+
34
+ print ("Orignal Array = " , A )
35
+
36
+ A = Bucket_Sort (A , 2 )
37
+
38
+ print (" Sorted Array = " , A )
You can’t perform that action at this time.
0 commit comments