Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. @staticmethod
  2. def radix_sort(a):
  3. """
  4. -------------------------------------------------------
  5. Performs a base 10 radix sort.
  6. Use: Sorts.radix_sort(a)
  7. -------------------------------------------------------
  8. Preconditions:
  9. a - an array of base 10 integers (list)
  10. Postconditions:
  11. Contents of a are sorted.
  12. -------------------------------------------------------
  13. """
  14. RADIX = 10
  15. maxLength = False
  16. tmp , placement = -1, 1
  17.  
  18. while not maxLength:
  19. maxLength = True
  20. # declare and initialize buckets
  21. buckets = [list() for _ in range(RADIX)]
  22. # split a between lists
  23. for i in a:
  24. tmp = int(i / placement)
  25. buckets[tmp % RADIX].append(i)
  26. if maxLength and tmp > 0:
  27. maxLength = False
  28.  
  29. x = 0
  30. for b in range(RADIX):
  31. bucket = buckets[b]
  32. for i in bucket:
  33. a[x] = i
  34. x += 1
  35. # move to next digit
  36. placement *= RADIX
  37. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement