|
| 1 | +# Bucket Sort |
| 2 | + |
| 3 | +*Sort elements using bucket* |
| 4 | + |
| 5 | +### How It Works: [Link to Video](https://www.youtube.com/watch?v=rNdTWHQMvOk) |
| 6 | + |
| 7 | +### Time Complexity |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +1. **Worst Case Complexity: O(n<sup>2</sup>)** |
| 12 | + When there are elements of close range in the array, they are likely to be placed in the same bucket. This may result |
| 13 | + in some buckets having more number of elements than others. |
| 14 | + It makes the complexity depend on the sorting algorithm used to sort the elements of the bucket. |
| 15 | + The complexity becomes even worse when the elements are in reverse order. If insertion sort is used to sort elements |
| 16 | + of the bucket, then the time complexity becomes O(n<sup>2</sup>). |
| 17 | +2. **Best Case Complexity: O(n+k)** |
| 18 | + It occurs when the elements are uniformly distributed in the buckets with a nearly equal number of elements in each |
| 19 | + bucket. |
| 20 | + The complexity becomes even better if the elements inside the buckets are already sorted. |
| 21 | + If insertion sort is used to sort elements of a bucket then the overall complexity in the best case will be linear |
| 22 | + ie. O(n+k). O(n) is the complexity for making the buckets and O(k) is the complexity for sorting the elements of the |
| 23 | + bucket using algorithms having linear time complexity at the best case. |
| 24 | +3. **Average Case Complexity: O(n)** |
| 25 | + It occurs when the elements are distributed randomly in the array. Even if the elements are not distributed |
| 26 | + uniformly, bucket sort runs in linear time. It holds true until the sum of the squares of the bucket sizes is linear |
| 27 | + in the total number of elements. |
| 28 | + |
| 29 | +### Space Complexity |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +1. The space complexity of Bucket Sort is O(n+k) where k is no of buckets |
| 34 | + |
| 35 | +### Properties |
| 36 | + |
| 37 | +1. May or may not be Stable |
| 38 | +2. Not Inplace |
| 39 | +3. Buckets are used to sort elements |
| 40 | + |
| 41 | +### Applications |
| 42 | + |
| 43 | +1. Input is uniformly distributed over a range. |
| 44 | +2. There are floating point values |
0 commit comments