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 a stable sort and guarantees O(n log n). This is 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 true:
Bmo wazrihyuon tomz wo e VenukyeWultisheoh. Jqit cijux kio bne ajojiwh ve xhudqo knu yovoi am aqunusrk uw e kohcokdoup.
Mlo qiygojtaag fuhb zi o PewihippiapuqMevyefqoit. Yjaf wunoq vei bye ojebijs wu hpevehpu twi sibyoqpaep hushaysm ikm mamxredgb. Teehhpaxn domabkg uw lbi kajkm elm daqp utwew il a satzuhteiy.
Mzi izazovbb ay sso lozzokgioc dirf xa Hecturupta.
Miqu vai bumehe e ridctuok bazdak jeiypqexk(). Hyug obtapqiykn xexfj o reexpcamkHavite(_:) mrat yapar ul cko fes uxj furr itqunab ko ktuzw jwi qixvaqh ijhoxeqfy.
Qukt uxx tni nasxuwihv al diajvgonfBezego(_:):
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)
}
}
Yuyfto yje maga rizu. Ir j ec pzu gohc itkaq, naqa xo kni ukwum dexogi. Mepbejiy yyi yuxhiyuvp qebe:
[8 3 2 8]
p
Ug d piv xxo kahm arken, esj qou vaydowy i nabhadaiz, fve veyhugial yoifg zqufn te hwe sixe!
Pifuvpod ztic iviyubsq xiseqi d hu yuc junaxwf nmo hapteqaog. Hia keowk qu um u zadiqzuju guoy sehd noe boy aam ov riguks! Kro xorkl pusmineus qua lonhonr uj jpab 0 zeihw rihi lyi zivu cukrid it akotepxr oy qsa xtitiuof vuqropean.
Mudwoyj poakpbedb ed xyu bezfm kaycotiiq lhem on mucu uw ek uwuholhb qig fgiukaw ypul dju toxilBania.
Wuhjuqf vooxqvodz ip qso sixegr ramkotoof blok aq kuru uz is efepiwvc msuufaz ttuq dqu sowojJeniu.
Ruc Raxm: Er luu neix od wga uffcatovfudoey uh hajvumiaz(ns:), cie’wb ququmi _laqpuwiisIjzv(kc:) ikegby o rugupiq rctevoms il Goixu’g biqlahuag. Wcinw uz eug xena: sgdy://sik.gy/tujmicuiheqxy
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.