Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import math
  2.  
  3. #array = [10, 20, -3, 4, 50, 6]
  4.  
  5. array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  6. k=10
  7. res = [[]]*k
  8.  
  9. def insertion_sort(res):
  10.  
  11. print(res)
  12.  
  13.  
  14. def scatter():
  15. minv = min(array)
  16. maxv = max(array)
  17.  
  18. bucket_size= math.ceil((maxv-minv)/k) #roundup or your last elements won't have a bucket to go to
  19. for x in array:
  20. #print(x)
  21. bucket_place = math.ceil(x/bucket_size)-1
  22. #print("place for ", x, "is ", bucket_place, "\n")
  23. res[bucket_place].append(x)
  24.  
  25. def insert_sort_buckets():
  26. for x in res:#sort each sublist
  27. insertion_sort(x)
  28.  
  29. def bucket_sort():
  30. scatter()
  31. insert_sort_buckets()
  32.  
  33. bucket_sort()
  34. print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement