Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- #array = [10, 20, -3, 4, 50, 6]
- array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- k=10
- res = [[]]*k
- def insertion_sort(res):
- print(res)
- def scatter():
- minv = min(array)
- maxv = max(array)
- bucket_size= math.ceil((maxv-minv)/k) #roundup or your last elements won't have a bucket to go to
- for x in array:
- #print(x)
- bucket_place = math.ceil(x/bucket_size)-1
- #print("place for ", x, "is ", bucket_place, "\n")
- res[bucket_place].append(x)
- def insert_sort_buckets():
- for x in res:#sort each sublist
- insertion_sort(x)
- def bucket_sort():
- scatter()
- insert_sort_buckets()
- bucket_sort()
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement