Pseudo Code
Lektion 4 von 9 im Kurs Radix Sort - DSA-Serie von Coddy.
countingSortByDigit(array, exp):
n = length(array)
output = array of size n
count = array of 10 zeros
for each x in array: # tally each digit
count[(x / exp) % 10] += 1
for d from 1 to 9: # running totals -> positions
count[d] += count[d - 1]
for k from n-1 down to 0: # place from the back (stable)
d = (array[k] / exp) % 10
count[d] -= 1
output[count[d]] = array[k]
return output
radixSort(array):
max = largest value in array
exp = 1
while max / exp > 0:
array = countingSortByDigit(array, exp)
exp = exp * 10- exp ist der Stellenwert: 1 für Einer, 10 für Zehner, 100 für Hunderter.
(x / exp) % 10extrahiert diese Ziffer. - countingSortByDigit ist eine stabile Sortierung für eine einzelne Ziffer: Sie zählt, wie oft jede Ziffer vorkommt, wandelt diese Zählwerte in Positionen um und platziert die Elemente dann von hinten nach vorne, um die Reihenfolge beizubehalten.
- radixSort führt diesen Durchlauf einfach einmal pro Ziffer aus und stoppt, wenn
expdie größte Zahl überschreitet.
Probier es selbst
Diese Lektion enthält keine Programmieraufgabe.
Diese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.
Alle Lektionen in Radix Sort - DSA-Serie
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)