Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
801 B

  1. def bucket_sort(alist):
  2. largest = max(alist)
  3. length = len(alist)
  4. size = largest/length
  5. buckets = [[] for _ in range(length)]
  6. for i in range(length):
  7. j = int(alist[i]/size)
  8. if j != length:
  9. buckets[j].append(alist[i])
  10. else:
  11. buckets[length - 1].append(alist[i])
  12. for i in range(length):
  13. insertion_sort(buckets[i])
  14. result = []
  15. for i in range(length):
  16. result = result + buckets[i]
  17. return result
  18. def insertion_sort(alist):
  19. for i in range(1, len(alist)):
  20. temp = alist[i]
  21. j = i - 1
  22. while (j >= 0 and temp < alist[j]):
  23. alist[j + 1] = alist[j]
  24. j = j - 1
  25. alist[j + 1] = temp
  26. alist = [54,26,93,17,77,31,44,55,20]
  27. print(bucket_sort(alist))