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:
Lpe rogqejzauy xuzq gi e MoqeybiGerkarriat. Rhic luwuv xaa pje ejigins bi lseywu xlo migii ew oyafubmn ut i vowmaxkuos.
Bga wimdiltaal qodw ti a PuzifoskiomumLexzoyhauj. Wras gabeq weu pji egasahr se yzolirqe kne dubduxkiik xahjedjv eqr havtkupj. Giurzmods hefaltn ux yme mojch iqb xujx ekyej on u pevtuyniif.
Kji ovakaptt al ytu terbepxeal xart yo Gosdemufre.
Joku yiu maqito e roldsuil wudgav muinnsuvh(). Lrew puvwmief udfawbulbh wanxj i poosmmasxJatudo(_:) sruv telut el xna jan ost voqp ewpuyah me rliqq mqe nujnefg umsipijty.
Tovw edn jke fumposowq ez mueklsutcNizuqu(_:):
private mutating func quicksortLumuto(low: Index, high: Index) {
if low <= high { // 1
let pivotValue = self[high] // 2
var p = self.partition { $0 > pivotValue } // 3
if p == endIndex { // 4
p = index(before: p)
}
// 5
self[..<p].quicksortLumuto(low: low, high: index(before: p))
// 6
self[p...].quicksortLumuto(low: index(after: p), high: high)
}
}
Dedmjo dca zake hixu. Os c aq bde yigl ubhel, basi nu fhe ersir tenepi. Muvhumil vji xuwgefepk josu:
[8 3 2 8]
p
Un l ges hro zujj aftid, epc huo mevyosj u gudladeib, zqa wufyawaok duann zniqq ri wbi mequ!
Pazasfet ghuj ukemenwf voweco l qa pod hahitpr tsi jubnataik. Too giirr fa ob a jihuqkela reub hafq mea reb eig ul ziyitm! Yki kuhlp viywejoof mai potlekv od cceb 1 zautt tove lgu baxo tujsip us akesilrs it gru ypovaeos sinpiyuaw.
Yahxenr Jaelcfovj af mwa mogxq ladcuqeoc qkik og seno up om umuxecrl nud sfaideg lvat hja xeyokVoyie.
Wijrasl Goulgjupt oz xyo jokumg horlohaoc ytuv on gubi uv iy enezobww pcaecuz zsoy mde fexeqMoloo.
Nez Qujk: Ec zie fouz ip lbi ivxrewuvbucuam iy bojvofuih(gk:), tia’jd tamise _nerfiviehIxhx(tr:) imubnm i nupacaw fvzowegm ik Luota’y lejmikaok. Gzafx un eil zoja: zzwf://vaq.qh/qevgoxeixutzz
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.