Chapter 25: Priority Queue


Actually in this chapter, it is mentioned that Swift default “sort” function uses “Introsort” in the background.
But with the release of Swift 5, this “sort” function started using “Timsort”.
And as this book is updated for Swift 5 already, so I thought to bring into ur notice for further correction.


1 Like

Challenge 2: Prioritize a waitlist

In challenge 2 solution, following sort function is given with an example -

 func tswiftSort(person1: Person, person2: Person) -> Bool {
  if person1.isMilitary == person2.isMilitary {
    return person1.age > person2.age
  return person1.isMilitary

@jomoka can u plz explain the logic of this sort, via stepwise diagrammatic representation or anything, which can help to understand it.

Challenge 3 solution is also not clear and I am not able to visualise the solution provided in the book. Plz help in this case too.

Thanks in advance for any kind of help to make it clear.

@kelvin_lau Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

I agree solution 3 is hard to visualize. I spent a lot of time trying to understand it.
One hint I can give is to think of it as you are “expanding reachable stations on each stop”. Starting with enqueuing reachable stations prioritized by chargeCapacity. (Reachable stations has distance less than your current charge.) Then you keep dequeuing a station with highest chargeCapacity, each time expanding reachable stations and increase minStops by 1. Keep doing this until target is reached.