|
1 | 1 | # Bucket Sort
|
| 2 | + |
2 | 3 | Bucket sort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm or by recursively applying the bucket sort algorithm. After all the individual buckets are sorted, the elements are concatenated together to form the sorted array.
|
3 | 4 |
|
4 | 5 | ## Complexity
|
| 6 | + |
5 | 7 | | Best | Average | Worst | Memory | Stable |
|
6 | 8 | |------|---------|-------|--------|--------|
|
7 | 9 | | log(n+k) | log(n+k) | log(n^2) | n | Yes |
|
8 | 10 |
|
9 |
| - |
10 | 11 | ## Pseudo Code
|
| 12 | + |
11 | 13 | ```
|
12 | 14 | BucketSort(arr, n):
|
13 | 15 | # Find the minimum and maximum values in the array
|
@@ -38,16 +40,16 @@ BucketSort(arr, n):
|
38 | 40 | ```
|
39 | 41 |
|
40 | 42 | ## Implementations
|
41 |
| -* [Python](#python)! |
42 |
| -* [C++](#cpp)! |
43 |
| -* [C](#c)! |
44 |
| -* [Java](#java)! |
45 |
| -* [JavaScript](#javascript) |
46 |
| -* [Go](#go)! |
47 |
| -* [Ruby](#ruby)! |
48 | 43 |
|
| 44 | +* [Python](#python) |
| 45 | +* [C++](#cpp) |
| 46 | +* [C](#c) |
| 47 | +* [Java](#java) |
| 48 | +* [JavaScript](#javascript) |
| 49 | +* [Go](#go) |
49 | 50 |
|
50 | 51 | ### Python
|
| 52 | + |
51 | 53 | ```python
|
52 | 54 | def bucket_sort(arr):
|
53 | 55 | # Find the minimum and maximum values in the input array
|
@@ -81,6 +83,7 @@ print(sorted_arr)
|
81 | 83 | ```
|
82 | 84 |
|
83 | 85 | ### CPP
|
| 86 | + |
84 | 87 | ```cpp
|
85 | 88 | #include <iostream>
|
86 | 89 | #include <vector>
|
@@ -128,7 +131,9 @@ int main() {
|
128 | 131 | }
|
129 | 132 |
|
130 | 133 | ```
|
| 134 | +
|
131 | 135 | ### C
|
| 136 | +
|
132 | 137 | ```c
|
133 | 138 | #include <stdio.h>
|
134 | 139 | #include <stdlib.h>
|
@@ -193,7 +198,7 @@ void bucketSort(double arr[], int size) {
|
193 | 198 | buckets[i] = NULL;
|
194 | 199 | }
|
195 | 200 | // Place each element in the appropriate bucket
|
196 |
| - for (int i = 0; i < size; i++) { |
| 201 | + for (int i = 0; i < numBuckets + 1; i++) { |
197 | 202 | int index = (int)((arr[i] - minVal) / bucketSize);
|
198 | 203 | insert(&buckets[index], arr[i]);
|
199 | 204 | }
|
@@ -222,6 +227,7 @@ int main() {
|
222 | 227 | ```
|
223 | 228 |
|
224 | 229 | ### Java
|
| 230 | + |
225 | 231 | ```java
|
226 | 232 | import java.util.ArrayList;
|
227 | 233 | import java.util.Collections;
|
@@ -271,6 +277,7 @@ public class BucketSort {
|
271 | 277 | ```
|
272 | 278 |
|
273 | 279 | ### JavaScript
|
| 280 | + |
274 | 281 | ```javascript
|
275 | 282 | function bucketSort(arr) {
|
276 | 283 | // Find the minimum and maximum values in the array
|
@@ -338,66 +345,67 @@ console.log(sortedArray);
|
338 | 345 | ```
|
339 | 346 |
|
340 | 347 | ### Go
|
| 348 | + |
341 | 349 | ```go
|
342 | 350 | package main
|
343 | 351 |
|
344 | 352 | import (
|
345 |
| - "fmt" |
346 |
| - "sort" |
| 353 | + "fmt" |
| 354 | + "sort" |
347 | 355 | )
|
348 | 356 |
|
349 | 357 | func bucketSort(arr []float64) []float64 {
|
350 |
| - if len(arr) == 0 { |
351 |
| - return arr |
352 |
| - } |
353 |
| - |
354 |
| - // Find the minimum and maximum values in the array |
355 |
| - minVal, maxVal := arr[0], arr[0] |
356 |
| - for _, num := range arr { |
357 |
| - if num < minVal { |
358 |
| - minVal = num |
359 |
| - } |
360 |
| - if num > maxVal { |
361 |
| - maxVal = num |
362 |
| - } |
363 |
| - } |
364 |
| - |
365 |
| - // Determine the range and the number of buckets |
366 |
| - rangeVal := maxVal - minVal |
367 |
| - numBuckets := len(arr) |
368 |
| - bucketSize := rangeVal / float64(numBuckets) |
369 |
| - |
370 |
| - // Create an array of empty buckets |
371 |
| - buckets := make([][]float64, numBuckets) |
372 |
| - for i := range buckets { |
373 |
| - buckets[i] = make([]float64, 0) |
374 |
| - } |
375 |
| - |
376 |
| - // Place each element in the appropriate bucket |
377 |
| - for _, num := range arr { |
378 |
| - index := int((num - minVal) / bucketSize) |
379 |
| - |
380 |
| - // Ensure that the index remains within a valid range |
381 |
| - if index == numBuckets { |
382 |
| - index-- |
383 |
| - } |
384 |
| - buckets[index] = append(buckets[index], num) |
385 |
| - } |
386 |
| - |
387 |
| - // Sort each bucket and concatenate them to get the sorted array |
388 |
| - sortedArr := make([]float64, 0, len(arr)) |
389 |
| - for _, bucket := range buckets { |
390 |
| - sort.Float64s(bucket) |
391 |
| - sortedArr = append(sortedArr, bucket...) |
392 |
| - } |
393 |
| - |
394 |
| - return sortedArr |
| 358 | + if len(arr) == 0 { |
| 359 | + return arr |
| 360 | + } |
| 361 | + |
| 362 | + // Find the minimum and maximum values in the array |
| 363 | + minVal, maxVal := arr[0], arr[0] |
| 364 | + for _, num := range arr { |
| 365 | + if num < minVal { |
| 366 | + minVal = num |
| 367 | + } |
| 368 | + if num > maxVal { |
| 369 | + maxVal = num |
| 370 | + } |
| 371 | + } |
| 372 | + |
| 373 | + // Determine the range and the number of buckets |
| 374 | + rangeVal := maxVal - minVal |
| 375 | + numBuckets := len(arr) |
| 376 | + bucketSize := rangeVal / float64(numBuckets) |
| 377 | + |
| 378 | + // Create an array of empty buckets |
| 379 | + buckets := make([][]float64, numBuckets) |
| 380 | + for i := range buckets { |
| 381 | + buckets[i] = make([]float64, 0) |
| 382 | + } |
| 383 | + |
| 384 | + // Place each element in the appropriate bucket |
| 385 | + for _, num := range arr { |
| 386 | + index := int((num - minVal) / bucketSize) |
| 387 | + |
| 388 | + // Ensure that the index remains within a valid range |
| 389 | + if index == numBuckets { |
| 390 | + index-- |
| 391 | + } |
| 392 | + buckets[index] = append(buckets[index], num) |
| 393 | + } |
| 394 | + |
| 395 | + // Sort each bucket and concatenate them to get the sorted array |
| 396 | + sortedArr := make([]float64, 0, len(arr)) |
| 397 | + for _, bucket := range buckets { |
| 398 | + sort.Float64s(bucket) |
| 399 | + sortedArr = append(sortedArr, bucket...) |
| 400 | + } |
| 401 | + |
| 402 | + return sortedArr |
395 | 403 | }
|
396 | 404 |
|
397 | 405 | func main() {
|
398 |
| - arr := []float64{3.2, 0.4, 2.8, 4.5, 1.1, 0.9} |
399 |
| - sortedArr := bucketSort(arr) |
400 |
| - fmt.Println(sortedArr) |
| 406 | + arr := []float64{3.2, 0.4, 2.8, 4.5, 1.1, 0.9} |
| 407 | + sortedArr := bucketSort(arr) |
| 408 | + fmt.Println(sortedArr) |
401 | 409 | }
|
402 | 410 |
|
403 |
| -``` |
| 411 | +``` |
0 commit comments