#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Sorting Algorithms

Bucket Sort in Data Structure

Bucket Sort works by distributing elements of an array into a number of 'buckets', sorting each bucket individually (often using Insertion Sort), and then concatenating the sorted buckets to produce the final sorted array.

Algorithm Steps

  • Create a fixed number of empty buckets.
  • Distribute each input element into a bucket based on its value (commonly by scaling to the number of buckets).
  • Sort each individual bucket, typically using Insertion Sort.
  • Concatenate all buckets in order to get the final sorted array.
def bucket_sort(arr):
    if not arr:
        return arr
    num_buckets = len(arr)
    max_val, min_val = max(arr), min(arr)
    range_val = (max_val - min_val) / num_buckets + 1e-9

    buckets = [[] for _ in range(num_buckets)]
    for num in arr:
        idx = int((num - min_val) / range_val)
        buckets[idx].append(num)

    result = []
    for bucket in buckets:
        bucket.sort()   # insertion sort could be used here instead
        result.extend(bucket)
    return result

Complexity

CaseTime
Best/Average (uniformly distributed data)O(n + k)
Worst (all elements in one bucket)O(n²)

Applications

  • Sorting uniformly distributed floating-point numbers (e.g., values between 0 and 1).
  • External sorting when data can be partitioned into ranges.
  • Used in histogram-based data processing.

Ready to master Data Structures & Algorithms?

Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.

Explore Course