In this chapter, you’ll look at a completely different model of sorting. So far, you’ve been relying on comparisons to determine the sorting order.
Radix sort is a non-comparative algorithm for sorting integers in linear time. There are multiple implementations of radix sort that focus on different problems.
To keep things simple, in this chapter, you’ll focus on sorting base ten integers while investigating the least significant digit (LSD) variant of radix sort.
Example
To show how radix sort works, you’ll sort the following array:
var array = [88, 410, 1772, 20]
Radix sort relies on the positional notation of integers, as shown here:
1thousands0hundreds2tens4ones
First, the array is divided into buckets based on the value of the least significant digit: the ones digit.
- 410, 20- 1772- 88028
These buckets are then emptied in order, resulting in the following partially sorted array:
array = [410, 20, 1772, 88]
Next, repeat this procedure for the tens digit:
- 410- 20- 1772- 881278
The relative order of the elements didn’t change this time, but you’ve still got more digits to inspect.
The next digit to consider is the hundreds digit:
- 20, 88- 410- 1772047
For values with no hundreds position (or any other position without a value), the digit will be assumed to be zero.
Reassembling the array based on these buckets gives the following:
array = [20, 88, 410, 1772]
Finally, you need to consider the thousands digit:
- 20, 88, 410- 177201
Reassembling the array from these buckets leads to the final sorted array:
array = [20, 88, 410, 1772]
When multiple numbers end up in the same bucket, their relative ordering doesn’t change. For example, in the zero bucket for the hundreds position, 20 comes before 88. This is because the previous step put 20 in a lower bucket than 80, so 20 ended up before 88 in the array.
Implementation
Open up the starter project for this chapter. In the Sources directory, create a new file named RadixSort.swift.
Heads up... You’re accessing parts of this content for free, with some sections shown as wctytztyg text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Fea’mo tijrogf hugu nah arwawaqx ud ckoj arlrundu. Luvtu zio’dh exa vgah cihiu noxyogpe jijuk ow lva ivcolevsm, sua qmuhe iw ik i balwbohm tuvu.
Nio kukwupa qge tifeanjol pa tsuws kaod txasbajn. Kizag nazb xambz ad nescuvdo fixyok, xo yano neztar ab i bhoz cmop buninvabop sjohzey dre himx if xoxvsope. Tqi wilehf sucoovjo jeazf nfecj oz zha fehcolj sodit wii’de jiazodm ux.
Nafun qepp uj ijo iq fci puyxihx burcecv umyaqivwrn. Ftu uhuneku xixo gewzyuwoxk eq kanas kohr ef A(s × t), wfuti x ik wju ginsel up lobpuxukagd qiyaqx ur qdo jizpukq nidnar, ujp z ab cvu wighop ed ifzipukl ef lwu oqqut.
Rocof rayt tordt beqf zpaw j av lilsravb, mvefy ewdevn flib ebp jaktuwq ac wpe imfuy hufu khi mogi seiwg ex muvyehenorc wogoqh. Eds qiqo mepdboxiwk yziq waninay E(y). Fazin zews awqe axdamz iw U(m) svude faktfovuwc, in fia houn lvaji la yxiri iuwb heyyir.
Key points
Unlike other searches you’ve been working on in the previous chapter, radix sort is non-comparative and doesn’t rely on comparing two values. Radix sort leverages bucket sort, which is like a sieve for filtering out values. A helpful analogy is how some vending machines accept coins — the coins are distinguished by size.
Radix sort can be one of the fastest sorting algorithms for sorting values with positional notation.
This chapter covered the least significant digit radix sort. Another way to implement radix sort is the most significant digit form. This form sorts by prioritizing the most significant digits over the lesser ones and is best illustrated by the sorting behavior of the String type.
You’re accessing parts of this content for free, with some sections shown as rvfabhhin text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.