1
1
# Bucket Sort
2
2
'''
3
- Bucket Sort is a sorting algorithm used to deal with input that is distributed over a range. It is a comparison sort algorithm.
4
- Here,the elements of the list are distributed into a number of buckets(or bins).
5
- Each bucket is then individually sorted using any sorting algoritm or by recursively applying the bucket algorithm.
6
- After sorting of each is bucket is completed, all the buckets are mergedand we geta sorted list.
3
+ Bucket Sort is a sorting algorithm used to deal with input that is distributed over a range.
4
+ It is a comparison sort algorithm.
5
+ The elements of the list are distributed into a number of buckets(or bins).Each bucket is
6
+ then sorted using any stable sorting algoritm.After sorting is complete, all the buckets are
7
+ merged and we get a sorted list.
7
8
Bucket Sort has a scatter-order-gather approach.
8
- '''
9
+ '''
9
10
10
11
11
12
def bucket_sort (arr ):
@@ -19,12 +20,12 @@ def bucket_sort(arr):
19
20
size = maximum / len (arr )
20
21
21
22
# Put list elements into their respective buckets i.e Scatter
22
- for i in range ( len ( arr ) ):
23
- j = int (arr [i ]/ size )
23
+ for index , element in enumerate ( arr ):
24
+ j = int (arr [index ]/ size )
24
25
if j != len (arr ):
25
- bucket_list [j ].append (arr [ i ] )
26
+ bucket_list [j ].append (element )
26
27
else :
27
- bucket_list [len (arr ) - 1 ].append (arr [ i ] )
28
+ bucket_list [len (arr ) - 1 ].append (element )
28
29
29
30
# Sorting each bucket individually and Merging
30
31
sorted_arr = []
@@ -41,7 +42,7 @@ def bucket_sort(arr):
41
42
42
43
def main ():
43
44
# Entering list of elements to be sorted
44
- arr = list (map (float ,input ("Enter sequence of comma separated numbers :\n " ).split (',' )))
45
+ arr = list (map (float , input ("Enter comma separated number sequence :\n " ).split (',' )))
45
46
46
47
sorted_list = bucket_sort (arr )
47
48
@@ -52,9 +53,9 @@ def main():
52
53
if __name__ == "__main__" :
53
54
main ()
54
55
55
- '''
56
- Sample I/O
57
56
57
+ # Sample I/O
58
+ '''
58
59
Enter sequence of comma separated numbers:
59
60
6.3,12.5,7.7,2.6,1.2,4.0
60
61
0 commit comments