# Bucket Sort in Data Structure

Mentor: Shailendra Chauhan
Duration : 00:03:00

## What is Bucket Sort in Data Structure?

Bucket Sort separates the unsorted array elements into multiple groups known as buckets. Each bucket is then sorted using one of the appropriate sorting algorithms or recursively using the same bucket algorithm. Finally, the sorted buckets are combined into a final sorted array.

## Scatter-Gather Approach

The scatter-gather strategy attempts to simplify difficult issues by first dispersing the entire data into groups or buckets, then solving these sub-problems independently before gathering them all together to provide the final answer.

## Bucket Sort Algorithm

• Make N buckets to store elements, each with a defined value range.
• Set each bucket to zero values.
• Distribute elements into buckets according to their range.
• Sort elements within each bucket using a different sorting technique or recursively applying bucket sort.
• To create the final sorted array, combine sorted elements from each bucket.

## Working of Bucket Sort

• The input array is separated into buckets, which indicate value ranges.
• Elements are arranged in buckets based on their values.
• Each bucket is sorted independently, either using a different sorting algorithm or recursively.
• To create the final sorted array, sorted elements are collected from each bucket.
• Efficiency is determined by element distribution across buckets and the sorting process used within each bucket.

## Complexity Analysis of Bucket Sort Algorithm

### Time Complexity

#### Best Case Complexity

O(n + k) occurs when the array is already sorted or the elements are evenly distributed in buckets. Using insertion sort on bucket elements results in linear complexity.

#### Average Case Complexity

The average case complexity is O(n + k) when the elements are jumbled, even with uniform distribution.

#### Worst Case Complexity

The worst-case complexity is O(n^2) when the items are near in range or reverse order, resulting in uneven bucket sizes.

### Space Complexity

The space complexity of bucket sort is O(n+k), where n is the number of elements in the array and k is the number of buckets created. Each bucket takes up O(k) space, and there are n components spread within it.

## Applications of Bucket Sort Algorithm

• Bucket Sort for Floating-Point Numbers: Divide floating-point numbers into buckets to sort them efficiently within a range.
• Bucket Sort for Integers: Excellent for sorting integers inside a specific range using bucket division.
• Known Data Distributions: Bucket sort is useful for sorting data having known distributions, such as uniform or Gaussian.
• Radix Sort Subroutine: This subroutine is commonly used in radix sort algorithms to sort numbers by digits, improving overall efficiency.

## Advantages of Bucket Sort Algorithm

• Efficient with Uniformly Distributed Data: Performs effectively when data is uniformly distributed.
• Simplicity: Simpler than quick sort or merge sort.
• Linear Time Complexity: Uniform distribution and small buckets achieve linear time complexity.

## Disadvantages of Bucket Sort Algorithm

• Limited Applicability: Performs best with well-distributed data; uneven distribution can result in imbalanced buckets and poor performance.
• Memory Requirements: Extra memory is required for constructing and storing buckets, depending on the input value range.
• Limited Non-Integer Sorting: Mostly for integers or floats; sorting other types may necessitate preprocessing or customization.

## Bucket Sort Algorithm Variations

There are the following bucket sort variations:

• Postman's Sort
• Histogram Sort
• Proxmap Sort
• Generic Bucket Sort
Still have some questions? Let's discuss.