Want more control over your search setup? Discover our flexible infrastructure pricing.

Go to homeMeilisearch's logo
Back to articles

A practical guide to understanding bucket sort

Learn what the bucket sort and recursive bucket sort algorithms are, how they work, and when you should use them through a practical example.

08 Aug 20235 min read
Carolina Ferreira
Carolina FerreiraDeveloper Advocate @ Meilisearch@CarolainFG
A practical guide to understanding bucket sort

What is bucket sort?

Bucket sort is a sorting algorithm that distributes elements from an array into several buckets. Once divided, each bucket is sorted individually, either using a different sorting algorithm or by applying the bucket sort method itself.

What is recursive bucket sorting?

Recursive bucket sort is a specific application of the bucket sort algorithm, where the bucket sort algorithm is used to sort the individual buckets. It is named "recursive" because the same method is used repeatedly to sort sub-buckets until the entire array is sorted.

What is bucket sorting used for?

Bucket sort is used for efficiently organizing data. By distributing the data into different buckets, it makes the later stages of sorting more efficient. It also allows to use different sorting algorithms within individual buckets. This flexibility allows for potential optimizations and makes bucket sort adaptable to various scenarios and data distributions.

What is the best and worst-case of bucket sort?

Bucket sort's performance varies greatly, with the best and worst cases depending on factors such as data distribution, size, and the choice of sorting algorithm used within individual buckets.

The best case of bucket sort

The best case for bucket sort happens when the data is spread evenly across the buckets. This uniform distribution minimizes the complexity of sorting each bucket, leading to an overall time complexity of O(n+k), where n represents the number of elements and k refers to the number of buckets.

The worst case of bucket sort

The worst case for bucket sort arises when all the elements end up in a single bucket, losing the advantage of distributing them across multiple buckets. This scenario leads to a time complexity determined by the sorting algorithm used for that single bucket. It can result in a worst-case time complexity of O(n2), depending on the algorithm.

How does a bucket sort algorithm work?

To better illustrate how it works, let's take the example of Meilisearch. Meilisearch is a search engine that uses bucket sort to sort search results on a set of consecutive rules called ranking rules.

For example, the `words` ranking rule sorts documents based on the number of words from the query found in the document.

Given the query “Badman dark knight returns”, the `words` rule will sort the returned documents into 4 buckets. These range from documents that contain all the words (possibly with a typo) to those that only contain the word “Badman”.

If `words` is the last ranking rule, or if the buckets only contain one document, then Meilisearch returns the buckets from the “best” (matching all words) to the “worst” (matching only one word). Documents matching 0 words are never returned as they have 0 relevancy to the search query.

buckets of documents after applying the words ranking rule on the query Badman dark knight returns Diagram illustrating bucket sorting using the words ranking rule.

Sorting documents using only a single ranking rule would force Meilisearch to either have a very complicated ranking rule or to perform a simplistic ranking. Meilisearch uses multiple ranking rules sequentially.

If a bucket from the first ranking rule contains multiple documents, the second ranking rule is used on that bucket to “break the tie” between documents. This technique could qualify as “recursive bucket sorting”. The sorting finishes when all the “innermost” buckets contain a single document, or after applying the last ranking rule.

How does a recursive bucket sort algorithm work?

To illustrate recursive bucket sorting, let’s imagine that we now added the `typo` ranking rule after the `words` ranking rule from the previous example. The `typo` ranking rule differentiates between documents that match the query directly and documents that would match if we were correcting one or two typos in the query. The former are better ranked than the latter.

Continuing with our sample query “Badman dark knight returns”, the `typo` ranking rule helps us further differentiate between documents from the last bucket. Note that it has no effect on the three other buckets, because they only contain documents such that the query has the typo “Badman -> Batman”.

buckets of documents after applying the words and typo ranking rules on the query Badman dark knight returns Diagram illustrating the application of recursive bucket sorting using the words ranking rule followed by the typo ranking rule.

By applying recursive bucket sorting to our query on the sample movies.json dataset, Meilisearch returns the ranking below. For simplicity, we have configured the dataset so that only `title` is a searchable attribute, which makes the results easier to grasp.

RankQuery wordsTypo correctionResulting documents
1"Badman dark knight returns""badman" → "batman"Batman: The Dark Knight Returns, Part 1
Batman: The Dark Knight Returns, Part 2
2"Badman dark knight""badman" → "batman"Batman Unmasked: The Psychology of the Dark Knight<br/>Legends of the Dark Knight: The History of Batman
3"Badman"No typoAngel and the Badman
4"Badman""badman" → "batman"Batman: Year One
Batman: Under the Red Hood

💡See bucket sort-powered relevancy in action in this Meilisearch demo.

If you want to try it yourself, you can create an account on Meilisearch Cloud and easily add and set up the movies dataset used to illustrate bucket sort. You can start searching in just a few minutes! Alternatively, you can use our open-source version and run Meilisearch locally. You just need to follow the instructions of our quick start guide.

🧪Experiment with bucket sorting by changing the order of the built-in ranking rules and adding your custom ones.

Conclusion

Bucket sort is a powerful and flexible algorithm, but its workings can be complex to understand without a concrete example. We hope this article has helped you gain a better understanding of what bucket sort is and how it functions.

Meilisearch is an open-source search engine that not only provides state-of-the-art experiences for end users but also a simple and intuitive developer experience.

For more things Meilisearch, you can join the community on Discord or subscribe to the newsletter. You can learn more about the product by checking out the roadmap and participating in product discussions.

Experience Bucket Sort in Action

See how Meilisearch leverages bucket sort and other advanced algorithms to deliver lightning-fast, relevant search results for your applications.

Semantic search vs Vector search: Key differences, uses, & more

Semantic search vs Vector search: Key differences, uses, & more

Discover the key differences between semantic and vector search, their use cases, strengths, and how to choose the right approach for your search needs.

Ilia Markov
Ilia Markov30 Jul 2025
Elasticsearch vs Typesense (vs Meilisearch): Which search engine actually fits your needs in 2025?

Elasticsearch vs Typesense (vs Meilisearch): Which search engine actually fits your needs in 2025?

Compare Elasticsearch's enterprise-scale power, Typesense's developer-friendly speed, and Meilisearch's AI-powered simplicity to find the search engine that matches your needs, team capabilities, and growth trajectory.

Ilia Markov
Ilia Markov29 Jul 2025
Most personalized search experiences are a lie

Most personalized search experiences are a lie

Learn what true search personalization looks like – and how to implement it without bloated, ineffective solutions.