π Sorting
Sort first, ask questions later. Many "hard" array problems become trivial once sorted. Know the sorts well enough to choose, and know when to use a custom comparator.
This category contains 21 problems. Use the patterns below to recognize what's being asked, then jump to the problem list at the bottom.
π§ Key Patternsβ
- Sort + Two Pointers β Pair/triplet sum, closest target.
- Counting / Bucket Sort β Integer values in known range β .
- Custom Comparator β Sort by frequency, by digit, "largest number" trick.
- Merge Sort β Count inversions, external sorting, stable sort.
- Quickselect β average for k-th smallest.
- Heap Sort / Top-K β Partial sort when full sort is overkill.
β οΈ Common Pitfallsβ
- Default sort in C# (
Array.Sort) is not stable;List<T>.Sortis also not stable. UseOrderBy(LINQ) for stable. - Comparator returning subtraction can overflow with
int.MinValue.
π Study Resourcesβ
πΊ Videosβ
π Booksβ
- Introduction to Algorithms (CLRS) β Ch. 2, 6β8
- Algorithms β Sedgewick β Ch. 2
π Articles & Referencesβ
π» All Sorting Problemsβ
Binary Gap
LeetCode 899 | Difficulty: Easy
Customer Placing the Largest Number of Orders
LeetCode 586 | Difficulty: Easy
Find Median from Data Stream
LeetCode 295 | Difficulty: Hard
Find the Kth Largest Integer in the Array
LeetCode 2113 | Difficulty: Medium
H-Index
LeetCode 274 | Difficulty: Medium
H-Index II
LeetCode 275 | Difficulty: Medium
Insert Interval
LeetCode 57 | Difficulty: Medium
Kth Largest Element in a Stream
LeetCode 789 | Difficulty: Easy
Kth Largest Element in an Array
LeetCode 215 | Difficulty: Medium
Largest Number
LeetCode 179 | Difficulty: Medium
Median of Two Sorted Arrays
LeetCode 4 | Difficulty: Hard
Meeting Rooms
LeetCode Link
Meeting Rooms II
LeetCode Link
Merge Intervals
LeetCode 56 | Difficulty: Medium
Sliding Window Median
LeetCode 480 | Difficulty: Hard
Sort Colors
LeetCode 75 | Difficulty: Medium
Sorting Algorithms
Insertion Sort
Top K Frequent Elements
LeetCode 347 | Difficulty: Medium
Top K Frequent Words
LeetCode 692 | Difficulty: Medium
Wiggle Sort
LeetCode Link
Wiggle Sort II
LeetCode 324 | Difficulty: Medium