Using Python Implement a radix sorting machine. A radix sort for base 10 integer
ID: 3593145 • Letter: U
Question
Using Python
Implement a radix sorting machine. A radix sort for base 10 integers is a mechanical sorting technique that utilizes a collection of bins, one main bin and 10 digit bins. Each bin acts like a queue and maintains its values in the order that they arrive. The algorithm begins by placing each number in the main bin. Then it considers each value digit by digit. The first value is removed and placed in a digit bin corresponding to the digit being considered. For example, if the ones digit is being considered, 534 is placed in digit bin 4 and 667 is placed in digit bin 7. Once all the values are placed in the corresponding digit bins, the values are collected from bin 0 to bin 9 and placed back in the main bin. The process continues with the tens digit, the hundreds, and so on. After the last digit is processed, the main bin contains the values in order.
Explanation / Answer
def radixsort( numbers ):
base = 10
isAllDigitProcessed = False
tmp = -1
placement = 1
while not isAllDigitProcessed:
isAllDigitProcessed = True
# declare and initialize bins
bins = [list() for _ in range( base )]
# split numbers between lists
for i in numbers:
# get remainning number before this pass
tmp = i // placement
bins[tmp % base].append( i )
if isAllDigitProcessed and tmp > 0:
isAllDigitProcessed = False
# empty lists into numbers array
a = 0
for b in range( base ):
bin = bins[b]
for i in bin:
numbers[a] = i
a += 1
# move to next digit
placement *= base
return numbers
numbers = [458, 534, 667, 5, 100]
print("Numbers before sorting " + str(numbers))
print("Numbers after radix sorting " + str(radixsort(numbers)))
# copy pastable code link: https://paste.ee/p/OxrUA
'''
Sample run:
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.