|
| 1 | +--- |
| 2 | +id: bucket-sort |
| 3 | +title: Bucket sort |
| 4 | +sidebar_label: Bucket sort |
| 5 | +tags: |
| 6 | + - DSA |
| 7 | + - Python |
| 8 | + - C++ |
| 9 | + - Java |
| 10 | + - Sorting |
| 11 | + |
| 12 | +description: "Thsi page containes Bucket Sort, with codes in python, java and c++ " |
| 13 | +--- |
| 14 | + |
| 15 | +### Introduction to Bucket Sort |
| 16 | + |
| 17 | +Bucket sort is a comparison sorting algorithm that distributes elements into a number of "buckets." Each bucket is then sorted individually, either using another sorting algorithm or recursively applying the bucket sort. Finally, the sorted buckets are combined to form the final sorted array. Bucket sort is particularly useful for uniformly distributed data. |
| 18 | + |
| 19 | +### Steps of Bucket Sort |
| 20 | + |
| 21 | +1. **Create Buckets**: Initialize an empty array of buckets. |
| 22 | +2. **Distribute Elements**: Distribute the elements of the input array into the appropriate buckets. |
| 23 | +3. **Sort Buckets**: Sort each bucket individually. |
| 24 | +4. **Concatenate Buckets**: Concatenate all sorted buckets to form the final sorted array. |
| 25 | + |
| 26 | +### Pseudocode |
| 27 | + |
| 28 | +```text |
| 29 | +function bucketSort(array, bucketSize): |
| 30 | + if length(array) == 0: |
| 31 | + return array |
| 32 | +
|
| 33 | + // Determine minimum and maximum values |
| 34 | + minValue = min(array) |
| 35 | + maxValue = max(array) |
| 36 | +
|
| 37 | + // Initialize buckets |
| 38 | + bucketCount = floor((maxValue - minValue) / bucketSize) + 1 |
| 39 | + buckets = array of empty lists of size bucketCount |
| 40 | +
|
| 41 | + // Distribute input array values into buckets |
| 42 | + for i from 0 to length(array) - 1: |
| 43 | + bucketIndex = floor((array[i] - minValue) / bucketSize) |
| 44 | + append array[i] to buckets[bucketIndex] |
| 45 | +
|
| 46 | + // Sort each bucket and concatenate them |
| 47 | + sortedArray = [] |
| 48 | + for i from 0 to bucketCount - 1: |
| 49 | + sort(buckets[i]) // You can use any sorting algorithm |
| 50 | + append buckets[i] to sortedArray |
| 51 | +
|
| 52 | + return sortedArray |
| 53 | +``` |
| 54 | + |
| 55 | +### Implementation in Python, C++, and Java |
| 56 | + |
| 57 | +#### Python Implementation |
| 58 | + |
| 59 | +```python |
| 60 | +def bucket_sort(numbers, size=5): |
| 61 | + if len(numbers) == 0: |
| 62 | + return numbers |
| 63 | + |
| 64 | + # Determine minimum and maximum values |
| 65 | + min_value = min(numbers) |
| 66 | + max_value = max(numbers) |
| 67 | + |
| 68 | + # Initialize buckets |
| 69 | + bucket_count = (max_value - min_value) // size + 1 |
| 70 | + buckets = [[] for _ in range(bucket_count)] |
| 71 | + |
| 72 | + # Distribute input array values into buckets |
| 73 | + for number in numbers: |
| 74 | + bucket_index = (number - min_value) // size |
| 75 | + buckets[bucket_index].append(number) |
| 76 | + |
| 77 | + # Sort each bucket and concatenate them |
| 78 | + sorted_numbers = [] |
| 79 | + for bucket in buckets: |
| 80 | + sorted_numbers.extend(sorted(bucket)) |
| 81 | + |
| 82 | + return sorted_numbers |
| 83 | + |
| 84 | +# Example usage |
| 85 | +data = [42, 32, 33, 52, 37, 47, 51] |
| 86 | +sorted_data = bucket_sort(data) |
| 87 | +print(sorted_data) # Output: [32, 33, 37, 42, 47, 51, 52] |
| 88 | +``` |
| 89 | + |
| 90 | +#### C++ Implementation |
| 91 | + |
| 92 | +```cpp |
| 93 | +#include <iostream> |
| 94 | +#include <vector> |
| 95 | +#include <algorithm> |
| 96 | +using namespace std; |
| 97 | + |
| 98 | +void bucketSort(vector<int>& nums, int bucketSize) { |
| 99 | + if (nums.empty()) |
| 100 | + return; |
| 101 | + |
| 102 | + // Determine minimum and maximum values |
| 103 | + int minValue = *min_element(nums.begin(), nums.end()); |
| 104 | + int maxValue = *max_element(nums.begin(), nums.end()); |
| 105 | + |
| 106 | + // Initialize buckets |
| 107 | + int numBuckets = (maxValue - minValue) / bucketSize + 1; |
| 108 | + vector<vector<int>> buckets(numBuckets); |
| 109 | + |
| 110 | + // Distribute input array values into buckets |
| 111 | + for (int num : nums) { |
| 112 | + int bucketIndex = (num - minValue) / bucketSize; |
| 113 | + buckets[bucketIndex].push_back(num); |
| 114 | + } |
| 115 | + |
| 116 | + // Sort each bucket and concatenate them |
| 117 | + nums.clear(); |
| 118 | + for (auto& bucket : buckets) { |
| 119 | + sort(bucket.begin(), bucket.end()); |
| 120 | + nums.insert(nums.end(), bucket.begin(), bucket.end()); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// Example usage |
| 125 | +int main() { |
| 126 | + vector<int> data = {42, 32, 33, 52, 37, 47, 51}; |
| 127 | + bucketSort(data, 5); |
| 128 | + for (int num : data) { |
| 129 | + cout << num << " "; |
| 130 | + } |
| 131 | + // Output: 32 33 37 42 47 51 52 |
| 132 | + return 0; |
| 133 | +} |
| 134 | +``` |
| 135 | +
|
| 136 | +#### Java Implementation |
| 137 | +
|
| 138 | +```java |
| 139 | +import java.util.ArrayList; |
| 140 | +import java.util.Collections; |
| 141 | +
|
| 142 | +public class BucketSort { |
| 143 | + public static void bucketSort(int[] array, int bucketSize) { |
| 144 | + if (array.length == 0) |
| 145 | + return; |
| 146 | +
|
| 147 | + // Determine minimum and maximum values |
| 148 | + int minValue = array[0]; |
| 149 | + int maxValue = array[0]; |
| 150 | + for (int num : array) { |
| 151 | + if (num < minValue) |
| 152 | + minValue = num; |
| 153 | + else if (num > maxValue) |
| 154 | + maxValue = num; |
| 155 | + } |
| 156 | +
|
| 157 | + // Initialize buckets |
| 158 | + int bucketCount = (maxValue - minValue) / bucketSize + 1; |
| 159 | + ArrayList<ArrayList<Integer>> buckets = new ArrayList<>(bucketCount); |
| 160 | + for (int i = 0; i < bucketCount; i++) { |
| 161 | + buckets.add(new ArrayList<Integer>()); |
| 162 | + } |
| 163 | +
|
| 164 | + // Distribute input array values into buckets |
| 165 | + for (int num : array) { |
| 166 | + int bucketIndex = (num - minValue) / bucketSize; |
| 167 | + buckets.get(bucketIndex).add(num); |
| 168 | + } |
| 169 | +
|
| 170 | + // Sort each bucket and concatenate them |
| 171 | + int currentIndex = 0; |
| 172 | + for (ArrayList<Integer> bucket : buckets) { |
| 173 | + Collections.sort(bucket); |
| 174 | + for (int num : bucket) { |
| 175 | + array[currentIndex++] = num; |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | +
|
| 180 | + // Example usage |
| 181 | + public static void main(String[] args) { |
| 182 | + int[] data = {42, 32, 33, 52, 37, 47, 51}; |
| 183 | + bucketSort(data, 5); |
| 184 | + for (int num : data) { |
| 185 | + System.out.print(num + " "); |
| 186 | + } |
| 187 | + // Output: 32 33 37 42 47 51 52 |
| 188 | + } |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +### Complexity |
| 193 | + |
| 194 | +- **Time Complexity**: |
| 195 | + |
| 196 | + - Best Case: $O(n + k)$, where $n$ is the number of elements and $k$ is the number of buckets. |
| 197 | + - Average Case: $O(n + k + n \log(\frac{n}{k}))$ |
| 198 | + - Worst Case: $O(n^2)$, when all elements are placed in one bucket and a slow sorting algorithm (like bubble sort) is used within buckets. |
| 199 | + |
| 200 | +- **Space Complexity**: $O(n + k)$, for the input array and the buckets. |
| 201 | + |
| 202 | +### Conclusion |
| 203 | + |
| 204 | +Bucket sort is efficient for sorting uniformly distributed data and can achieve linear time complexity in the best case. However, it may degrade to quadratic time complexity in the worst case if elements are not uniformly distributed. It's essential to choose an appropriate bucket size and secondary sorting algorithm for optimal performance. By understanding its structure and implementation, you can effectively use bucket sort for various sorting tasks. |
0 commit comments