import random
random.seed(0)
def bucket_sort(mylist):
# initialize the buckets
lst = []
for i in range(10):
lst.append([])
print(lst)
length = len(mylist)
# place the values to be sorted in the buckets
for i in range(len(mylist)):
ind = int(mylist[i]/length)
if mylist[i] < 100:
lst[ind].append(mylist[i])
else:
ten = [100]
print(lst)
# sort each bucket
result = []
# concatenate your bucket to the result
return result
I have a list of 100 numbers and I wanted to split them into 10 buckets (0-9,10-19, etc.). But the numbers won't go into each bucket I assigned
As
lengthislen(mylist), it will be 100 for your input. As the code only adds numbers to buckets when they are less than one hundred, the value ofindwill always be 0. The division was of something less than 100 divided by 100...A quick fix would be to set:
Other remarks
It is strange to do
ten = [100]. This listtenis never used, it makes little sense to do this repeatedly, and its value does not necessarily correspond to an actual value in the input.The index of the bucket should really be determined by the range of the values in the input. If it is given that the input will have numbers between 0 and 99, then it is fine to divide by 10, but if the input could have numbers between (let's say) -5000 and 5000, then you need to calculate the bucket index differently. You need the distance between the greatest and least value in the input element, to derive the range of a single bucket.
Finally, there are still some things to do in your code:
implement the part where it says to sort the buckets. You can use the same function recursively.
concatenate the buckets
Here is a spoiler implementation: