Typorithm · hard

Bucket Sort

▷ Interactive algorithm labbucket-sortsortingdistributionbucketsinsertion-sort
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Bucket Sort

0%

Sorting Algorithms

Bucket Sort distributes values into range buckets, sorts each bucket, and concatenates the buckets.

Bucket Sort relies on values being reasonably distributed over a known numeric range.

The min and max values determine how many buckets are needed.

Each input value maps to a bucket based on its distance from min and the bucket size.

Buckets are sorted individually; this version uses Insertion Sort because buckets should be small.

When distribution is good, Bucket Sort can approach linear time; when buckets are uneven, it depends on the inner sort.

Preparing algorithm lab…
01

Choose a bucket size.

02

Map values into range buckets.

03

Sort each bucket locally.

04

Flatten buckets from low range to high range.

05

Best when values are evenly distributed.

function bucketSort(arr, bucketSize = 5) {
  if (arr.length <= 1) return arr;

  const min = Math.min(...arr);
  const max = Math.max(...arr);
  const bucketCount = Math.floor((max - min) / bucketSize) + 1;
  const buckets = Array.from({ length: bucketCount }, () => []);

  for (const value of arr) {
    const index = Math.floor((value - min) / bucketSize);
    buckets[index].push(value);
  }

  return buckets.flatMap((bucket) => insertionSort(bucket));
}

function insertionSort(bucket) {
  for (let i = 1; i < bucket.length; i++) {
    const current = bucket[i];
    let j = i - 1;

    while (j >= 0 && bucket[j] > current) {
      bucket[j + 1] = bucket[j];
      j--;
    }

    bucket[j + 1] = current;
  }

  return bucket;
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Small input

Small arrays need no bucket work.

if (arr.length <= 1) return arr

2. Minimum

The minimum anchors the first bucket.

const min = Math.min(...arr)

3. Maximum

The maximum helps compute how many buckets are needed.

const max = Math.max(...arr)

4. Bucket count

The value range divided by bucket size determines the number of buckets.

const bucketCount = Math.floor((max - min) / bucketSize) + 1

5. Bucket array

Create a list of empty buckets.

Array.from({ length: bucketCount }, () => [])

6. Bucket index

Shift by min, divide by bucket size, and floor to choose a bucket.

const index = Math.floor((value - min) / bucketSize)

7. Place value

Values in the same range go into the same bucket.

buckets[index].push(value)

8. Sort and flatten

Sort each bucket, then concatenate buckets in order.

return buckets.flatMap((bucket) => insertionSort(bucket))

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Bucket Sort: Visual Walkthrough & Practice · Typorithm | ApplyDjinn