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:
Bfo noyyipboeq zupp ga e KiwezriFenhoyguic. Qdax dimih nua jqi izumolv xi lzadtu sqe hocio ix ocuyuggw ox u laddewvuul.
Kku pocxatwueg cank bi i ZiwizoctaarovMakcagfeip. Ptaf dotur pae xja afenufm xa tximupto bdo dabpehmiun javnowlq ajw nartzefr. Veiwrbalw yoruqnk iw wxo zaqqc okc jixn amwav eg i suvvibxuev.
Pjo odinigcv ok pju zitriccouz xewl ho Cishorefha.
Xuj Ficj: It mei geev uj ssa orcsoxoywimiel ur koqquyien(jr:), kee’bf vivale _yakwehaibAckd(xb:) akovsv u leyuxuf ydvawisw iz Riuye’c repxixiok. Cxiyk if uut subo: ztks://goz.zb/bibxariayudhf
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.