Sorting: Put It in Order
Sorting: Put It in Order 🔢
Binary search was amazing — but it only worked because the list was already sorted. Somebody has to DO that sorting. Time to learn how order actually happens.
Checking order
Order lives between NEIGHBORS. A list is sorted when every item is ≤ the next one:
ok = True
for i in range(len(nums) - 1):
if nums[i] > nums[i + 1]:
ok = False
Making order
The swap is your chisel:
nums[i], nums[j] = nums[j], nums[i]
In this unit you'll build three real sorting algorithms with your own hands — bubbling neighbors, repeatedly rescuing the smallest, and sliding cards into place. They have official names (bubble sort, selection sort, insertion sort), but you'll know them as things you built.
Then, the shortcut
Once you've sweated, you get the reward: sorted(nums) and nums.sort(), plus their magic words reverse=True and key=. And then the best part — sorting as a tool: duplicates become neighbors, closest pairs become adjacent, "third biggest" becomes a lookup, and fair-splitting puzzles crack wide open.
Sort first, think second. It's a strategy, and it wins constantly. Let's go!