AI Hints
Home›DSA in Python Tutorial›Simple Sorts
DSA in Python · Lesson 49 of 132

Why Sorting Matters

Order makes everything easier.

⏱ 2 min read▶ Video at 56:56🎓 Free
Watch this lesson in the DSA in Python courseWatch on YouTube ↗Full DSA in Python course

Sorted data is easier to work with. You can binary search it, find duplicates next to each other, and spot the smallest and largest instantly. Sorting is so useful that computer scientists have invented dozens of ways to do it.

Why Sorting Matters
From the video: Why Sorting Matters
  • Binary search needs it
  • Duplicates sit together
  • Dozens of ways to sort

What sorting unlocks

Once data is sorted, many problems get easy. Finding the median is just the middle item. Duplicates are neighbours. Two pointers work. And merging lists is simple. Learning how sorting works also teaches you the big ideas of algorithms, like comparing, swapping, and divide and conquer.

Binary searchO(log n) lookups
Medianthe middle item
Duplicatesend up next to each other
Big ideasdivide and conquer

Sorting in Python

Python can already sort for you. Sorted returns a new sorted list, and the original is unchanged. Dot sort sorts the list in place. Reverse equals true sorts from big to small. Run it. Over the next lessons, we'll build sorting algorithms ourselves, to see what's happening inside.

sorting.pypython
nums = [5, 2, 9, 1, 7]
print(sorted(nums), nums)

nums.sort()
print(nums)
print(sorted(nums, reverse=True))
Output
[1, 2, 5, 7, 9] [5, 2, 9, 1, 7]
[1, 2, 5, 7, 9]
[9, 7, 5, 2, 1]

Summary

Sorting makes searching, medians, and duplicates easy. Python sorts for you, but knowing how sorting works is a core skill. Next, the simplest sort of all.

Key takeaways
  • sorted() makes a copy
  • list.sort() is in place
  • Sorting unlocks other tricks
Next up: Bubble Sort — swap neighbours