Sorting and Searching
LockedLesson 22 of 60 · DataStructures · Medium
Mastery0% of 85%
Up next: Variables and Types (~7 min) →Finish these first:Recursion →Lists →Or pass the tests below to unlock it now.
1
The big idea
sorted() and list.sort() are stable O(n log n). Binary search needs sorted data and runs in O(log n).
Think of it like this: Think of data structures as specialized toolboxes: a List keeps items in numbered slots (`0, 1, 2...`), a Set keeps only unique badges, and a Dictionary/Map labels each compartment with a custom name key.
Key words (tap to learn):
2
Read the lesson
sorted() and list.sort() are stable O(n log n). Binary search needs sorted data and runs in O(log n).
sorted(words, key=len, reverse=True)
import bisect
bisect.bisect_left(xs, target)
3
Quick check
Before writing code: What should `binary_search([1,3,5,7],5)` return when your implementation of Sorting and Searching is working?
4
2 sample tests, 1 hiddenYour challenge
Write binary_search(xs, target) returning the index or -1.
Stuck? Get a hint (free)