DryRun
AlgorithmsCourseCompare
DryRun
AlgorithmsCourseCompareGitHubLinkedIn

© 2026 DryRun. Built by Aditya Pandey.

AlgorithmsSortingBucket Sort
Sortingintermediatesortingnon-comparisondistribution-sort

Bucket Sort

Bucket Sort divides input domain into buckets, places each element into its bucket, sorts each bucket (often with insertion sort), then concatenates buckets in order. It is linear on average for uniformly distributed input.

Complexity

Best
O(n + k)
Average
O(n + k)
Worst
O(n^2)
Space
O(n)

Implementation

vector<double> bucketSort(vector<double> arr) {
    int n = arr.size();
    vector<vector<double>> b(n);
    for (double x : arr) {
        int idx = min(n - 1, (int)(x * n));
        b[idx].push_back(x);
    }
    for (auto& bk : b) sort(bk.begin(), bk.end());
    vector<double> out;
    for (auto& bk : b) for (double x : bk) out.push_back(x);
    return out;
}

How It Works

1.Distribution Step

Map each element into one of k domain buckets.

2.Local Sort

Sort each bucket independently; small buckets are cheap.

3.Input Sensitivity

Uniform distributions give near-linear behavior; skew can degrade performance.

Related Algorithms

ArrowUpDownCounting SortArrowUpDownRadix Sort

Test Your Knowledge

Quiz coming soon for this algorithm.

Back to all algorithms