Activity Selection Implementation in Swift
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Activity Selection runs in O(n log n) time and O(1) space, in Swift as in every other language.
- ✓Array.sorted(by:) (non-mutating, returns a new array) or Array.sort(by:) (mutating), with a trailing closure comparing on finish: activities.sorted { $0.finish < $1.finish }.
- ✓Use sorted(by:), not sort(by:), unless you specifically own and intend to mutate the array you were given.
- ✓The Swift trap: A plain (Int, Int) tuple type does not conform to Comparable, Swift does not allow tuple types to conform to protocols at all, so activities.sorted() with no closure on an array of (Int, Int) fails at COMPILE time with a protocol-conformance error, never as a runtime surprise.
- ✓sort(by:)/sorted(by:) is a stable, adaptive merge sort with a guaranteed O(n log n) worst case, true of both the implementation and the documented guarantee since Swift 5.
What Activity Selection actually is
Activity Selection in Swift: what you actually reach for
Swift implementation
struct Activity {
let start: Int
let finish: Int
}
func selectActivities(_ activities: [Activity]) -> [Activity] {
let ordered = activities.sorted { $0.finish < $1.finish } // non-mutating
var selected: [Activity] = [ordered[0]]
var lastFinish = ordered[0].finish
for activity in ordered.dropFirst() {
if activity.start >= lastFinish {
selected.append(activity)
lastFinish = activity.finish
}
}
return selected
}
let activities = [Activity(start: 1, finish: 4), Activity(start: 3, finish: 5),
Activity(start: 0, finish: 6), Activity(start: 5, finish: 7),
Activity(start: 8, finish: 9), Activity(start: 5, finish: 9)]
print(selectActivities(activities))
// [Activity(start: 1, finish: 4), Activity(start: 5, finish: 7), Activity(start: 8, finish: 9)]Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Swift-specific trap
Performance in Swift, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Swift type or library should I use for Activity Selection?
Array.sorted(by:) (non-mutating, returns a new array) or Array.sort(by:) (mutating), with a trailing closure comparing on finish: activities.sorted { $0.finish < $1.finish }. No import beyond the standard library is needed.
What is the most common Swift mistake when implementing Activity Selection?
A plain (Int, Int) tuple type does not conform to Comparable, Swift does not allow tuple types to conform to protocols at all, so activities.sorted() with no closure on an array of (Int, Int) fails at COMPILE time with a protocol-conformance error, never as a runtime surprise. You must supply by: on every sort call over a tuple element, which is also why defining a real Activity struct (as below) instead of an unlabeled tuple is the idiomatic choice: it reads better at every call site for the identical underlying reason.
How does Activity Selection actually perform in Swift?
sort(by:)/sorted(by:) is a stable, adaptive merge sort with a guaranteed O(n log n) worst case, true of both the implementation and the documented guarantee since Swift 5. Because Array has copy-on-write value semantics, calling the MUTATING sort(by:) on an array that has any other live reference triggers a full buffer copy before the sort even starts, so 'mutate in place' is only actually cheaper than sorted(by:) when the array happens to be uniquely referenced, something a caller can't see from the call site alone. Time complexity is O(n log n) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Replit free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.