Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def counting_sort(arr, exp1):
- n = len(arr)
- output = [0] * n
- count = [0] * 10
- for i in range(n):
- index = (arr[i] / exp1)
- count[int(index % 10)] += 1
- for i in range(1, 10):
- count[i] += count[i - 1]
- i = n - 1
- while i >= 0:
- index = (arr[i] / exp1)
- output[count[int(index % 10)] - 1] = arr[i]
- count[int(index % 10)] -= 1
- i -= 1
- for i in range(len(arr)):
- arr[i] = output[i]
- return arr
- def radix_sort(arr):
- array = arr.copy()
- max1 = max(array)
- exp = 1
- while max1 / exp > 0:
- array = counting_sort(array, exp)
- exp *= 10
- return array
- arr = [1, 2, 112, 18, 56, 985, 36, 521, 1024]
- arr = radix_sort(arr)
- print(*arr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement