From f92ec3be6c94d0d7079f39e15afe317564f29b73 Mon Sep 17 00:00:00 2001 From: mahi_korrapti Date: Fri, 14 Nov 2025 21:48:57 +0530 Subject: [PATCH 1/2] Add documentation for bucket sort --- src/sorting/bucket_sort.md | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/sorting/bucket_sort.md diff --git a/src/sorting/bucket_sort.md b/src/sorting/bucket_sort.md new file mode 100644 index 0000000..01321e5 --- /dev/null +++ b/src/sorting/bucket_sort.md @@ -0,0 +1,43 @@ +# Bucket Sort + +## Overview +Bucket Sort is a distribution-based sorting algorithm that divides input elements into buckets and sorts each bucket individually. + +## Steps +1. Create `n` empty buckets. +2. Distribute numbers into buckets based on value ranges. +3. Sort each bucket individually. +4. Merge all buckets to form the final sorted list. + +## Time Complexity +| Case | Time | +|------|------| +| Best | O(n + k) | +| Average | O(n + k) | +| Worst | O(n²) | +| Space | O(n + k) | + +## Rust Example + +```rust +pub fn bucket_sort(arr: &mut [f32]) { + let n = arr.len(); + let mut buckets: Vec> = vec![Vec::new(); n]; + + for &value in arr.iter() { + let index = (value * n as f32) as usize; + buckets[index.min(n - 1)].push(value); + } + + for bucket in buckets.iter_mut() { + bucket.sort_by(|a, b| a.partial_cmp(b).unwrap()); + } + + let mut idx = 0; + for bucket in buckets { + for value in bucket { + arr[idx] = value; + idx += 1; + } + } +} From be70be6cd63e99ce867f091018f2d2dbe00978e9 Mon Sep 17 00:00:00 2001 From: mahi_korrapti Date: Mon, 17 Nov 2025 18:47:48 +0530 Subject: [PATCH 2/2] docs: remove example --- src/sorting/bucket_sort.md | 43 -------------------------------------- 1 file changed, 43 deletions(-) diff --git a/src/sorting/bucket_sort.md b/src/sorting/bucket_sort.md index 01321e5..e69de29 100644 --- a/src/sorting/bucket_sort.md +++ b/src/sorting/bucket_sort.md @@ -1,43 +0,0 @@ -# Bucket Sort - -## Overview -Bucket Sort is a distribution-based sorting algorithm that divides input elements into buckets and sorts each bucket individually. - -## Steps -1. Create `n` empty buckets. -2. Distribute numbers into buckets based on value ranges. -3. Sort each bucket individually. -4. Merge all buckets to form the final sorted list. - -## Time Complexity -| Case | Time | -|------|------| -| Best | O(n + k) | -| Average | O(n + k) | -| Worst | O(n²) | -| Space | O(n + k) | - -## Rust Example - -```rust -pub fn bucket_sort(arr: &mut [f32]) { - let n = arr.len(); - let mut buckets: Vec> = vec![Vec::new(); n]; - - for &value in arr.iter() { - let index = (value * n as f32) as usize; - buckets[index.min(n - 1)].push(value); - } - - for bucket in buckets.iter_mut() { - bucket.sort_by(|a, b| a.partial_cmp(b).unwrap()); - } - - let mut idx = 0; - for bucket in buckets { - for value in bucket { - arr[idx] = value; - idx += 1; - } - } -}