Sorting as a
Solution Strategy
Learn to recognize when sorting data first transforms hard problems into simple ones: reducing complexity from \(O(n^2)\) to \(O(n \log n)\) and unlocking powerful algorithmic techniques.
What Is Sorting?
Sorting means rearranging elements into a defined order: ascending, descending, alphabetical, or by any comparison rule. In C++, std::sort handles this in \(O(n \log n)\) average time.
Why Sort First?
Sorting creates structure in raw data. That structure lets you replace brute-force nested loops with elegant single-pass or binary-search techniques.
The Sorting Advantage Visually
Consider the problem: "Find if any two elements sum to a target." Without sorting, you check every pair. With sorting, two pointers solve it in one pass.
The Core Principle
Sorting in C++
Sorted vs Unsorted Approaches
| Problem | Without Sorting | With Sorting |
|---|---|---|
| Find duplicates | \(O(n^2)\) nested loops | \(O(n \log n)\) sort + linear scan |
| Two-sum target | \(O(n^2)\) check all pairs | \(O(n \log n)\) sort + two pointers |
| Find median | \(O(n^2)\) naive selection | \(O(n \log n)\) sort + index \(n/2\) |
| Closest pair | \(O(n^2)\) all distances | \(O(n \log n)\) sort + neighbors |
| Count inversions | \(O(n^2)\) nested comparison | \(O(n \log n)\) merge sort variant |
How to Apply the Strategy
Example 1: Finding Duplicates
Problem: Given an array of integers, determine if any value appears more than once.
Example 2: Two-Sum with Two Pointers
Problem: Given a sorted array and a target \(T\), find two elements that sum to \(T\).
Example 3: Greedy Scheduling
Problem: Given \(n\) tasks with deadlines and durations, maximize tasks completed on time. Sort by deadline, then greedily pick tasks.
When Not to Sort
Sorting is powerful but not universal. There are situations where it destroys the information you need or is overkill.
Patterns Unlocked by Sorting
Common Mistakes
Summary Table
| Signal | Technique After Sort | Total Complexity |
|---|---|---|
| "Find if X exists" | Binary search | \(O(n \log n)\) |
| "Find a pair summing to T" | Two pointers | \(O(n \log n)\) |
| "Find duplicates" | Adjacent comparison | \(O(n \log n)\) |
| "Schedule tasks optimally" | Greedy sweep | \(O(n \log n)\) |
| "Find k-th smallest" | Direct index access | \(O(n \log n)\) |
| "Merge overlapping intervals" | Linear merge | \(O(n \log n)\) |
Question 1 of 5
You need to check whether an array of \(n\) integers contains any duplicate values. Which approach is most efficient?
Question 2 of 5
After sorting an array, which algorithmic technique lets you find two elements that sum to a target in \(O(n)\) time?
Question 3 of 5
Why is it wasteful to sort an array just to find its minimum element?
Question 4 of 5
You have a list of meeting intervals [(1,3), (5,8), (2,6), (9,10)]. To merge overlapping intervals efficiently, what should you do first?
Question 5 of 5
A problem asks you to find the longest increasing subsequence (LIS) of an array. Should you sort the array first?