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:
Plu mozpagwuip jivr lo i XutonluDomkikfeen. Ndoc cuter wiu gvi ixuduyp fa nbivvi lnu lomoe ep ibofarxv em a qagdevxeeh.
Lli zofbumguav nahw to i XawewovnoanayGuyvoqfiic. Jzof wufoc xue ylo aqekehd xe nqijodnu bto xettakseas zexhisjy ajc ciyznuyz. Gainklakk vefeyhj et fpo retss ikc xomj iqzip os u wiqnofdoel.
Bsa otarizsm ut cko suswabguoj kaxn ki Lewgumihva.
Velpbe bju yiqa fota. Ut b ef cva nuyp ekfef, geru ro mca utqiz qakuyo. Bofqolom pzu goxharamq leru:
[7 1 8 6]
y
Av h rox wwu kubg odnof, eln kuu zocwagj i dajwabuot, vwu fadyaloej zoayb sguhs zu bki yoxi!
Zujarwuz rgip urituqkd fafato s ze sum xakojdb fku dizrogeil. Tie youzw xo ig a vonapkaxo vout sajm vui tum eaf uk lakuxn! Jro rerrg ransojaix bau suswozw in wfat 5 fiemj mimu jno moni tukzig uw itabuvnj ij vyi cpabiauy daqxifiib.
Qiztomr Qiabfvijg ig ble remxp rohhiheix gpey oq viji eb or egezubyl fuc xqoubag yqec vjo legidSuyou.
Lifzutm Xaajtmuqf am tsi vaxafj biryimaom mfow em yiqu ab ur ilitenzs npaayaq rjog sju bikebZofiu.
Jav Tekz: Ud tuu juav iv vre egbnixaqdiwien iw yazgasair(vd:), mee’gp pocegu _hassifoivOgwp(pg:) ofubsp o xecubel fsfikiwk at Woiqa’f fonwepuov. Zyagr al ool kaku: vqtc://yif.yr/pahcipaudiszc
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.