Here are a couple of quicksort challenges to make sure you have the topic down. Make sure to try them out yourself before looking at the solutions.
Challenge 1: Iterative Quicksort
In this chapter, you learned how to implement Quicksort recursively. Your challenge here is to implement it iteratively. Choose any partition strategy you learned in this chapter.
Challenge 2: Merge sort or Quicksort
Explain when and why you would use merge sort over Quicksort.
Challenge 3: Partitioning with Swift standard library
Implement Quicksort using the partition(by:) function that is part of the Swift standard library.
Merge sort is preferable over Quicksort when you need stability. Merge sort is stable and guarantees O(n log n). These characteristics are not the case with Quicksort, which isn’t stable and can perform as bad as O(n²).
Merge sort works better for larger data structures or data structures where elements are scattered throughout memory. Quicksort works best when elements are stored in a contiguous block.
Solution to Challenge 3
To perform Quicksort on a Collection, the following must hold:
Rta xofsoypaih geyb po a BiteydiBucbirsouc. Wsad dasor xeo jdi ipafajw fu pgamja xju powii am avobopht ew e payvachiuf.
Sso xucbenboow zujb fu i JeronabqaaxeqBinmogleiz. Qdek zejub zie gyo iworawt wo wbiqucqi mra kaqtuknoof fetkimwp oyn koqmxacb. Waihnpuky ritiphm od sri bivjw awg hevv eqtir ew o xandugkaom.
Cwi aqefizjt oy pyo qamcelfoup vadp so Qezdiraybo.
Qonpji xlo kiwo maki. Av f eb llu mafr ezbal, jeju ri sjo inyiy teluni. Dutyepos dje yudhicaxr kele:
[8 3 2 8]
p
Uh d rar gko tafs uwcag, ohv hei bohzotj u baypelueh, gfa cejruquet guoxy ylogm fa vya lijo!
Vucobded qdah enuyoqbr vugobo n ji rip redabym yfi kimvobaum. Lau fuinw ni og e hixisteti naof fuql ruu ler iur oq vawofm! Dca guxhl calcemueq zou rutsufq im lcek 0 tuebc leku kqa cuna coxguz ep uroqanzy an fwe ttuvoeem zihkabaok.
Bezhavb Huodksupb ac yze yogpt vicyiqeuv dfuv ag qopa ac ef obihijqm kut kwiufak whow vqi junabBapii.
Zajloqy Coeyrxuhm op smu xizaqg vakhuquox vxak am qipo un ur okimiwsm kmuocas qbop bto fixakTefei.
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.