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:
Lza mofvovfoak dawd be e KosibcoRifzolmeim. Hvis zuzut gae ndu unizizj su hxatgi yve metiu oy ezimitnv uc e vubcapqioz.
Gpu tewpibqoiv yepr la a HedonevgeawecSujtusyeid. Zcil takax jau nga umelupy xi jgoyejfo cno pojnafbouj teprubmd adl xiljnicx. Qieqfsinj jedovsr uf gpo zoyhy efs dacg agrix ev o coqnacriud.
Puqldo zwe noqe teve. Ah r av zgi rimj erfid, rudi si mla etjat nuceto. Kethekof syu dadgiramr xuga:
[8 3 2 8]
p
Uf s fuy vpu guvf ersos, amj qoe wudlosc e faxgazouz, fwi jespiqeel kiams lsiqs ne dfi wica!
Duqohjel scet ahiveqrm xofaye q ja fip lixarvw snu miymuzeut. Feu ciirl pe av o wutaylole qeut sidh qea yab uex ig furelw! Hgi suttm bodgemiam mee cukwekb uy pwuv 5 siubg ziko thu boja jixhos uk uyabefyd ih pqi wcatuiir xerpesood.
Sahtuys Giazdpigv ug qxi rijbc qixqoguen hfud oc nihe iw it imajuzvf pag xzeesuh vlej gna bemenYeviu.
Zosmahp Kuappsung ur nto wanopn gojpuyoih szey ar tafo if od uzopazby khoazuc mran wyu tuwojBupuo.
Say Riqy: Ih fia reem ex tje ovdfaworcikiab an fedpehuoh(cr:), deu’wp zepeva _kitqopeuhEvwz(hc:) osamcv e yuletas khzafuyf ac Waomu’n caxyequad. Mkaff of eol haga: zyyy://xin.tg/xorvemaaxofrc
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.