Skip to content

Commit afb695f

Browse files
authored
Update Bucket_sort.py
1 parent e9d623f commit afb695f

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Python/sort/Bucket_sort.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Bucket Sort
22
'''
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.
78
Bucket Sort has a scatter-order-gather approach.
8-
'''
9+
'''
910

1011

1112
def bucket_sort(arr):
@@ -19,12 +20,12 @@ def bucket_sort(arr):
1920
size = maximum / len(arr)
2021

2122
# 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)
2425
if j != len(arr):
25-
bucket_list[j].append(arr[i])
26+
bucket_list[j].append(element)
2627
else:
27-
bucket_list[len(arr) - 1].append(arr[i])
28+
bucket_list[len(arr) - 1].append(element)
2829

2930
# Sorting each bucket individually and Merging
3031
sorted_arr = []
@@ -41,7 +42,7 @@ def bucket_sort(arr):
4142

4243
def main():
4344
# 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(',')))
4546

4647
sorted_list = bucket_sort(arr)
4748

@@ -52,9 +53,9 @@ def main():
5253
if __name__ == "__main__":
5354
main()
5455

55-
'''
56-
Sample I/O
5756

57+
# Sample I/O
58+
'''
5859
Enter sequence of comma separated numbers:
5960
6.3,12.5,7.7,2.6,1.2,4.0
6061

0 commit comments

Comments
 (0)