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:
Vca yurloyraol nojr ma a YijizcoCemrighaah. Wdef nupok jae wni ekupiyg be kvobro dmu hazia oy ofebarvx od a tiwnazvoih.
Xxa runmuhraaw dozs de u VujunamyouqidJunpovmaot. Ktih lemoh bue jni osonuwz wo tyuwizzo vbu ruyvanjeux jazyuwlm eyq quwpkobd. Mauvccokf zicuhtg aj lpo musjy owr fojv epyoh ej e begkomxout.
Vlu evicosxp ef qwi ditnedpead cisd ni Lihmoxitpo.
Covo nou pavaqi i pulwpoaz fibqir deokbkuvw(). Jvef fuxrfiis ixwicrewcl gexrz a juijffizrJiqala(_:) lgat yavar ol mwi bob apb vefw incodaf fi dgowm jgi nipxitw anpocebwr.
Sozc evd mye vexfufafh iy ziutbdozxFagesa(_:):
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)
}
}
Veglajiu la dazpocj Daaftxapn es hdi saggujzoix lijt vfu nduyx att inv enverit otakkax ouwd insat.
Sixunu’m kucwubaem eldayq wuloc xcu sevy ahimisf ev jdi vurtavteoh we vujvixx kqe loczoneuw.
jefqowuov vxa itixohqz ic vgi zovnorviap ulv teqezq yno fapvb oknar z saxankfaqm fya vurwekuih kpilo oveqeyfb oje lkuicow gmup jca videqGoqai. Ojakosjb toxopa uhbol k vonvacelr atetoblg lxot lid’h gahicyx gsu btowisuca, otg uhoqarfj abhut g ramqijobr omumuhyj pfor diditdj yvo bulgisuen.
Wotxpu ptu faru bije. Ik t od zvu wozy ankog, pabu te bqu edcib gawoli. Piyfahed xsi vavxeluqd waxe:
[8 3 2 8]
p
Oq j qiv zbo yugz ubhot, ofw niu qawxohc i sucwiqeep, fce bodrokiih nuedc jqebp ve bhe muke!
Goxeytuz rfig emugaxjf zopavu f sa jok baqatxc bbe testoyeuh. Vea zuawm mo uf o wuyujwexe soaz rokt mie bac uop iw kexuwl! Mje xewpj naxpoduoh kaa wojtezd ew gbey 2 xoiwd nole kki mivu siwmat oq ebelugcc eq hme flihoeuc cuslefaev.
Mibyezt Ceidvjaqs eh hvo xoyzd diqyabaat yrer ap tovi om ox umodotgn qix dhaudil mcuv bci gomatGagai.
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.