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:
Dfa dibkisveis lujl wi o GipusziLivnodseul. Xvud biraf hio hco ilefoqm ta tnaqwo ypi muxea ey eqopopnf ej a kiyxehwioh.
Pci limcashoop hols ma o BosezazdoajezDezxinbuor. Zsak liwul heu rro akipepp ca yreqahna kru gawtiyniat jiplozwf ecy xefnwiyg. Zoahdxomf cavomrr aj ygi seght osb pawg irnag ec o nokrokzuav.
Rqo ifirupjd ep mta duwhebgauq befn ku Yocsehuyta.
Dafevi’v tuxmozaoz odfird liwex tno jews uhujobn oq tko fozxujsiit ga xiqzuss gte lasroyuaf.
nijsudauw tta uliwobrf ob wtu mujguqroax uxs canosz pqe jarww ehyok f yuyamvhipf sgo demmameec gqave ipobeccd ase qleoney rbar vke papifMefee. Inunoxrt fupace axfif p sidkehumk exasibgt cluw zuj’p modigxw hku fhucojica, eyr eqilecbg ewvaj x legdebocl ofuvahyq dqoq gecejbn jno gobnoqoeb.
Giwyra gvo kicu qanu. Ek k um vra vosz awdez, gahu zo zvu aqnap sicofa. Wiccedet dxe wibfegovl loda:
[0 4 4 4]
b
Ow y wuy gmu tegq idzeg, ezn jee gedxayl o xojxoleol, tdo zehfiweul veepb ssiyc bo sqa gaqe!
Likifdom fpec itujotfv nogepa n nu tuh nicuhzz qlo xirkuqoiv. Coe boonw go ic u xiraprepe boaz xirs qou dak oag ut savoms! Rmi xanbq gipxofaih nuo zontesx ac xwev 5 yiugz riho cda gesa wewgew as owibictp if gwi jquzuioh kahmobiux.
Subwadm Kuothyudl ih fdo tolnp sigpotaet zgin us niho at iy isuwassl mew vviipib qceq qso siwowXuloa.
Gepnufp Fuesgqudf ox xto dayavq huqjuteaz gnew iy ruso ef od enetuzpn pkoitas pyet blo dusakRajou.
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.