Bucket sort distributes the input values into a number of buckets, sorts each bucket on its own and then concatenates the buckets in order. In C you implement it with an array of linked lists (or dynamic arrays) and a small sort such as insertion sort inside each bucket. It runs in O(n + k) expected time when the values are spread evenly over k buckets, but degrades to O(n^2) if most values land in one bucket. It needs O(n + k) extra memory and is stable when each bucket is sorted stably.
Bucket sort suits data with a known range and an even distribution, such as floating-point numbers in [0, 1) or scores from 0 to 100.
k empty buckets, each covering an equal slice of the value range.bucket = (int)(value * k) for values in [0, 1).Because every value in bucket i is smaller than every value in bucket i + 1, sorting inside the buckets is enough to sort the whole array.
This version keeps each bucket as a singly linked list and inserts new values in sorted position, so the buckets are already sorted when the scan ends.
Points worth noting:
calloc gives zeroed memory, so every bucket starts as a NULL list.malloc result is checked, and all nodes are freed even on failure.<=, keeps equal elements in input order.bucket_sort requires values in [0, 1). Values outside the range must be rejected or scaled first.For integers in [0, max], map each value with value * k / (max + 1). Use long long or size_t in the product to avoid overflow.
With scores from 0 to 100 and 10 buckets, a score of 57 maps to bucket 5 and a score of 100 maps to bucket 9. If every value in the range occurs and the range is small, counting sort is simpler and faster than bucket sort.
| Case | Time | Explanation |
|---|---|---|
| Best | O(n + k) | Values spread evenly, tiny buckets |
| Average | O(n + k) | Uniform distribution |
| Worst | O(n^2) | All values in one bucket and insertion sort inside it |
| Space | O(n + k) | Nodes plus the bucket array |
| Stable | Yes | When insertion keeps equal items in order |
The worst case is the reason bucket sort is not a general-purpose sort. Skewed data, such as many identical values, puts almost everything in one bucket. Using an O(n log n) sort inside each bucket caps the worst case at O(n log n).
k close to n so the average bucket holds about one value.| Algorithm | Average time | Needs known range | Extra space |
|---|---|---|---|
| Bucket sort | O(n + k) | Yes, and even spread | O(n + k) |
| Counting sort | O(n + range) | Yes, integers | O(range) |
Quicksort (qsort) | O(n log n) | No | O(log n) |
| Merge sort | O(n log n) | No | O(n) |
For general data in C, call qsort from <stdlib.h>. Reach for bucket sort when the distribution is known to be even.
Expected O(n + k) for evenly distributed input, and O(n^2) in the worst case when one bucket receives nearly everything.
It can be. Append or insert so equal values keep their input order, and use a stable sort inside each bucket.
Buckets are expected to hold only a few items, and insertion sort is fast and simple on small lists. It also keeps stability.
Yes, after shifting. Subtract the minimum value from every element, or use min and max to compute the bucket index as (value - min) * k / (max - min + 1).
Bucket sort splits values into range-based buckets, sorts each bucket and joins them. In C, use an array of linked lists, check every allocation and free all nodes. It is O(n + k) for even data and O(n^2) in the worst case, so use it only when the distribution is known and otherwise prefer qsort.