14. Sorting

Merge sort, quick sort, counting/bucket sort and custom comparators.

🎬 Episodes 24–28: video coming soon
Your progress0 of 11
3easy
6medium
2hard

Understand the topic

Sorting puts order into data so that later steps become easy. Merge sort splits in half, sorts both halves and merges them, always O(n log n). Quick sort partitions around a pivot. Counting and bucket sort beat n log n when values come from a small range.

In Java, Arrays.sort(int[]) sorts primitives, while Arrays.sort(T[], comparator) and list.sort(...) take a custom order. Many hard problems are "merge sort plus counting while you merge".

#ProblemPatternLevel
1 Height Checker #1051 Counting sort Easy LeetCode β†—
2 Sort Array by Increasing Frequency #1636 Custom comparator Easy LeetCode β†—
3 Relative Sort Array #1122 Counting sort Easy LeetCode β†—
4 Sort an Array #912 Merge sort Medium LeetCode β†—
5 Sort List #148 Merge sort on a linked list Medium LeetCode β†—
6 Largest Number #179 Comparator on concatenation Medium LeetCode β†—
7 Merge Intervals #56 Sort by start Medium LeetCode β†—
8 Custom Sort String #791 Counting by custom order Medium LeetCode β†—
9 Maximum Gap #164 Bucket sort (pigeonhole) Medium LeetCode β†—
10 Count of Smaller Numbers After Self #315 Merge sort with indexes Hard LeetCode β†—
11 Reverse Pairs #493 Merge sort counting Hard LeetCode β†—

Work top to bottom β€” each list goes from easy to hard. Try a problem for at least 15 minutes before opening a hint. Ticks are saved in your browser.