Sorting

Bucket Sort

Size:15
Execution Narrative
Step 1 of 39Start
Line 1
Starting Bucket Sort on array of 15 elements.
Comparisons28
Swaps0
Operations28

Overview & Concept

Bucket Sort distributes elements into a set of uniformly partitioned "buckets". Each bucket is then sorted individually using Insertion Sort or recursively, and the contents of the buckets are concatenated in order. It achieves linear average-case O(n + k) time for uniformly distributed data.

How It Works

Range Partitioning: Divides the value interval into k uniform sub-intervals (buckets).

Scatter: Distributes each array element into its corresponding bucket based on index = ⌊n × A[i]⌋.

Individual Bucket Sorting: Sorts each bucket using an efficient stable algorithm like Insertion Sort.

Gather: Concatenates all non-empty buckets sequentially to form the sorted result.

Step-by-Step Execution

1. Initialize BucketsCreate an array of k empty bucket lists.
2. Scatter ElementsPlace each element into its corresponding bucket based on normalized value.
3. Sort Each BucketApply Insertion Sort to each individual bucket.
4. Gather ResultsIterate through buckets and append elements sequentially into original array.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(n + k)O(n + k)O(n²)O(n + k)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
procedure bucketSort(A):
  find min and max; create k empty buckets
  for each x in A:
    compute bucketIndex = floor((x - min) / range * (k - 1))
    insert x into bucket[bucketIndex]
  for each bucket:
    sort bucket individually using insertion sort
  concatenate all buckets sequentially back into A
  return A
end procedure
Algorithmic Invariants
  • Bucket Separation Invariant: All elements in bucket i are strictly less than all elements in bucket i + 1.
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Linear average time O(n + k) for uniformly distributed data.
  • •Supports floating-point numbers seamlessly.

Trade-offs & Limitations

  • •Degenerates to O(n²) if data is clustered into a single bucket (poor distribution).
  • •Requires auxiliary memory for bucket collections.

Real-World Applications

Uniform Floating-Point Sorting

Sorting floating-point numbers distributed uniformly in the range [0, 1).

Geographic Location Binning

Clustering GPS coordinates into spatial grid cells.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When input numbers are floating-point values known to be uniformly distributed.

Consider Alternatives When

  • ✕When data has severe clustering or high variance.

Compare trade-offs and complexity across complementary tools in AlgoViz.