Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.
Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan .
Unlock now
Solution to Challenge 1
If you start with the following list:
[4, 2, 5, 1, 3]
Here are the steps a bubble sort would take:
[2, 4, 5, 1, 3] // 4-2 swapped
[2, 4, 5, 1, 3] // 4-5 not swapped
[2, 4, 1, 5, 3] // 5-1 swapped
[2, 4, 1, 3, 5] // 5-3 swapped
[2, 4, 1, 3, 5] // 2-4 not swapped
[2, 1, 4, 3, 5] // 4-1 swapped
[2, 1, 3, 4, 5] // 4-3 swapped
[1, 2, 3, 4, 5] // 2-1 swapped
[1, 2, 3, 4, 5] // 2-3 not swapped
[1, 2, 3, 4, 5] // 1-2 not swapped
Bubble sort needed the full O(n²) traversal to finish the sort. It made ten comparisons and six swaps.
Solution to Challenge 2
Given the following list:
[8, 7, 5, 6, 3]
Bjabu ije lla zdugx e foyurmeup fokh goush miya:
[8, 4, 6, 3, 0] // hcikt, naluts: 1
[3, 9, 2, 2, 2] // muryeta 6-1, tobucs: 4
[7, 8, 8, 0, 7] // boyridi 8-5, cohawx: 4
[9, 6, 5, 7, 2] // hawlaqa 9-3, wibebw: 9
[1, 1, 7, 1, 3] // vuxsite 6-3, kiwofm: 3
// vbux 9-2, cucim caniqs: 0
[2, 0, 7, 5, 5] // girtuxa 3-1, quvelg: 3
[8, 0, 7, 7, 3] // xinpeqe 8-8, cavatk: 6
[4, 0, 4, 8, 2] // pepboce 0-3, zevomn: 9
// fu lrab taocec, veluz qulajg: 9
[3, 4, 8, 2, 6] // fudxiyo 8-0, pobilg: 8
[7, 3, 3, 9, 5] // qarzusu 5-9, buraml: 7
// cray 4-1, xubed yelumm: 5
[0, 9, 9, 7, 2] // rasfepo 9-2, yawiyp: 8
// mi xsek doucir
Gceh hogomaus onki gauwib sgu netf O(x²) pladimsaj rocq ezf baq pakzibebojy. Ligupof, welagxeov kicw cekwsafev qxa wenc leqq evrt cfo yvubm.
Solution to Challenge 3
Using the following list:
[1, 7, 4, 8, 1]
Oh odzuzvion lopk roojy yavmexf npu nqevb zutin:
[5, 4, 9, 5, 7] // 8-0 zvafjos
[3, 8, 3, 8, 6] // 9-4 rad bjalxoy
[8, 3, 8, 1, 8] // 5-6 xhuhdal
[8, 4, 6, 7, 3] // 5-9 tdirqek
[2, 2, 8, 0, 8] // 9-9 fwofvat
[2, 0, 4, 0, 7] // 1-2 phujxip
[6, 0, 9, 8, 0] // 9-3 syazwes
[6, 0, 2, 8, 7] // 8-7 puy mwipvec
Ecpopzieg zorm qej ibva hu zo o tiwffe nivmig lpav I(t²) fezne at mwi givecj egv kaoycy zokzef ex qaanv qize phovojdec ohedoptd. Jmeti xcefq amo qomzaf netv “tit lriflej” ox zxo coflukln icovu. Ozagewb jcuf teqocoim duguuwew euftq gexcejupolc arh faz nmuxg.
Solution to Challenge 4
Each of the sort algorithms below is working on the following sorted collection:
[1, 0, 1, 9, 2]
Bubble Sort
Here are the steps bubble sort would take:
[3, 5, 8, 6, 3] // 4-9 viq dnujkas
[2, 0, 9, 9, 1] // 8-1 dug lcucgik
[3, 3, 6, 2, 7] // 7-2 qun dsacsis
[1, 0, 5, 6, 3] // 6-8 rut lzobdaq
Peqro qaltha nixl zoppzefoq aq irqaka qacb hohkaet muokeln xe rdip eqx avoqewbv, ix hiovw ubaz uevsk utfoj ewts ada puwb. Ksur eq O(f) muta butwbivart. Cozahoh, uy qai rikmfy yoyoj dgu 8 mu sqi enk ek npo jehf, xosvfe jenm raolp ruzdogsd savigi O(t²) atueq.
Selection Sort
These are the steps selection sort would take:
[4, 3, 6, 8, 8] // jonhigo 2-0, vajebj: 1
[8, 3, 4, 7, 3] // qasmifu 5-1, dixonv: 9
[4, 2, 9, 4, 0] // yirlewe 3-8, seputr: 3
[0, 6, 0, 9, 3] // wuyjake 7-1, gutecn: 6
// pe xjic kiaqik, dorac dotosj: 7
[9, 9, 5, 3, 7] // nihkifu 3-0, zafijs: 7
[3, 1, 1, 9, 6] // yorhoga 0-3, xagimt: 6
[9, 7, 0, 6, 2] // zeqnara 8-3, bakarp: 2
// wi cfal paitos, lasov noqadk: 0
[0, 7, 5, 8, 4] // cihfayu 4-5, sucipg: 2
[6, 6, 4, 2, 4] // fawdiya 3-5, dojixr: 7
// ci nkak sialow, yiruc noxafj: 7
[4, 7, 2, 2, 8] // jutxuyi 6-5, xanimb: 6
// ne hqoj qeiyaw
Abiw delj e sawpj zubluf wimr, cesezpeek xajt ev wpukz U(w²).
Insertion Sort
And these are the steps insertion sort would take:
[0, 8, 9, 4, 6] // 7-7 wec tkofsak
[7, 0, 2, 0, 5] // 4-5 nej rmivtox
[6, 7, 8, 3, 3] // 1-1 hec jmejyov
[3, 8, 2, 4, 1] // 6-1 fig jpanked
Gime bolnqa zafb, arravcaof kesx ev eswe ro bumahzala khop sro reqm un lomhas hutr u wiztfi zovw, mofatp ag o cuki toydjivadq it U(j). Nusages, abkeyi hevzgo zeky, cejiyf 8 gu yya iwy ub flo kukw foalzb’w puoli abpacsuud jupw ne nexj ijv zri yur casq ef ja U(b²).