MemotivaLeetCode Patterns Flashcards: Arrays and Hashing, Frequency Maps, Duplicates

What is the bucket sort pattern and when does it outperform comparison sorting?

LeetCode Patterns Flashcards: Arrays and Hashing, Frequency Maps, Duplicates

Аудио-карточка · 0:29

Nortren·

What is the bucket sort pattern and when does it outperform comparison sorting?

0:29

The bucket sort pattern distributes elements into buckets based on value ranges, then processes each bucket separately. It achieves O of n time when the range of values is bounded, bypassing the O of n log n lower bound of comparison-based sorting. Use it when you need the k-th most frequent element: create buckets indexed by frequency, place elements into their frequency bucket, then scan buckets from highest to lowest. It also works for problems with bounded value ranges like sorting colors or ages. Recognize it when frequency or bounded values allow grouping instead of comparing.
neetcode.io