Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/sorting/bucket_sort.md
Original file line number Diff line number Diff line change
@@ -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<f32>> = 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;
}
}
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this example please.

Suggested change
## Rust Example
```rust
pub fn bucket_sort(arr: &mut [f32]) {
let n = arr.len();
let mut buckets: Vec<Vec<f32>> = 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;
}
}
}

Loading