Algorithm Atlas · worked example
See the algorithm, step by step
Distribute values into range buckets, sort locally, and concatenate in bucket order.
This call is starting. Local values appear as they are initialized.
Data & variables
Changed Source indexExample input
[0.9,0.1]Follow the code. Data appears here when it enters scope.
Call stack · 1 frame
maincall 1
Rust source
Line 30fn csv<T: std::fmt::Display>(a: impl IntoIterator<Item = T>) -> String {
a.into_iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(",")
}
fn bucket_sort(values: &[f64]) -> Vec<f64> {
if values.is_empty() {
return Vec::new();
}
let n = values.len();
let mut buckets = vec![Vec::new(); n];
for &x in values {
assert!((0.0..1.0).contains(&x));
buckets[(x * n as f64) as usize].push(x);
}
for bucket in &mut buckets {
for i in 1..bucket.len() {
let mut j = i;
while j > 0 && bucket[j] < bucket[j - 1] {
bucket.swap(j, j - 1);
j -= 1;
}
}
}
buckets.into_iter().flatten().collect()
}
fn main() {
let result = csv(bucket_sort(&[0.9, 0.1]));
println!("{result}");
}
Try your own version
Change the independent fixture `[0.9,0.1]` in a legal way, then compare the result with your prediction. Explain how the same invariant applies to the separate visible challenge `[0.42,0.32,0.23,0.52,0.25]`.
Open personal copy in Playground ↗Start with this exact source and input, change a value, then use Run or Debug locally. This replay stays attached to the original example.
Recordings use a native Rust target. Playground runs on wasm32-wasip1; target-sized types such as usize can have different limits.

Understand it
Values are distributed across buckets without a large concentration in one bucket. The worked program demonstrates this invariant on an independent fixture, so you can study the implementation before predicting the challenge result.
For teaching input [0.9,0.1], the worked program returns 0.1,0.9. Its functions are complete and use only std. Adapt the same state transitions to the visible challenge input [0.42,0.32,0.23,0.52,0.25]; predict that result before opening the answer reveal. The precise contract is: Values are decimals in [0,1). Distribute them into n buckets by floor(n*x), where n is the input length, sort each bucket, and return all values ascending.
Return canonical output: lowercase booleans, decimal integers without leading zeros, comma-separated values and semicolon-separated groups, with no extra whitespace or final newline. Whole medians use integer form and half medians use .5. Textual sentinels such as none and # retain their stated meaning. String and character-grid fixtures use ASCII unless the lesson explicitly specifies Unicode scalar values.
Solve the exact task, not just the visible example: Values are decimals in [0,1). Distribute them into n buckets by floor(n*x), where n is the input length, sort each bucket, and return all values ascending. On input [0.42,0.32,0.23,0.52,0.25], predict the output before opening the answer reveal. The revealed output is a test oracle for this case, while private checks exercise other legal cases. Start by stating the input grammar and output format in your own words. Parse the complete input, including surrounding whitespace, before relying on any convenient positions in the example.
Build the solution around one maintained claim: Distribute values into range buckets, sort locally, and concatenate in bucket order. Decide what state represents that claim, how it is initialized, what a single update does, and when the algorithm stops. Sorting establishes global order; selection asks only for a rank or partition. Explain which comparisons make progress toward that goal. Trace the visible input by hand and record at least two intermediate states, not only the final answer. If the trace cannot justify your predicted output, there is a gap between the idea and the implementation.
Check both correctness and cost. The intended bound is O(n+k) average · O(n^2) worst · O(n+k) space; explain which operation dominates it and how many times the input can trigger that operation. Test all-equal keys, already ordered input, reverse order, and extreme pivot or bucket distributions. Add one small case that should succeed, one boundary case, and one case chosen to challenge the invariant. Compare your hand result with the program's result, then use a failing case to fix the earliest incorrect state transition rather than patching the final output.
The pattern
Distribute values into range buckets, sort locally, and concatenate in bucket order.
When to use it: roughly uniform numeric distributions
Complexity: O(n+k) average · O(n^2) worst · O(n+k) space
Input: [0.42,0.32,0.23,0.52,0.25]
Read the code
fn csv<T: std::fmt::Display>(a: impl IntoIterator<Item = T>) -> String {
a.into_iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(",")
}
fn bucket_sort(values: &[f64]) -> Vec<f64> {
if values.is_empty() {
return Vec::new();
}
let n = values.len();
let mut buckets = vec![Vec::new(); n];
for &x in values {
assert!((0.0..1.0).contains(&x));
buckets[(x * n as f64) as usize].push(x);
}
for bucket in &mut buckets {
for i in 1..bucket.len() {
let mut j = i;
while j > 0 && bucket[j] < bucket[j - 1] {
bucket.swap(j, j - 1);
j -= 1;
}
}
}
buckets.into_iter().flatten().collect()
}
fn main() {
let result = csv(bucket_sort(&[0.9, 0.1]));
println!("{result}");
}
Complete std-only bucket-sort program for its typed teaching fixture. Parsing and output construction are separate from the core algorithm.
Practice starter
pub fn solve(input: &str) -> String {
// Values are decimals in [0,1). Distribute them into n buckets by floor(n*x), where n is the input length, sort each bucket, and return all values ascending.
// Trim surrounding whitespace; implement parsing and the algorithm.
let _ = input;
todo!("implement bucket-sort")
}
Try it
Values are decimals in [0,1). Distribute them into n buckets by floor(n*x), where n is the input length, sort each bucket, and return all values ascending.
What success looks like: The private harness prints PASS bucket-sort. It checks at least five distinct semantic inputs plus surrounding-whitespace variants. Explain complexity separately; this harness does not time the algorithm.
Check your understanding
Boundary check for bucket-sort: with input [0.01,0.99], what result should solve return?
Reveal the answer
0.01,0.990.99,0.010,1
For this input the result is 0.01,0.99. Values are distributed across buckets without a large concentration in one bucket. Compare this independent fixture with the worked trace: Values 0.42,0.32,0.23,0.52,0.25 enter buckets determined by floor(n*x). Sorting within each bucket and concatenating them yields 0.23,0.25,0.32,0.42,0.52. Concentrating many values in one insertion-sorted bucket can cost quadratic time.
Go deeper
Common misconception: “Every input distribution guarantees linear time”
Values are distributed across buckets without a large concentration in one bucket. The independent teaching fixture demonstrates this rule; predict the separate visible challenge before revealing its answer.
- Pattern-specific invariant: Values are distributed across buckets without a large concentration in one bucket. This is the property the named implementation maintains; explain which comparison, state update or recursive return in the worked program establishes it.
- Independent typed fixture: The worked call uses
[0.9,0.1]and computes0.1,0.9. Keep parsing separate, then identify the core state before and after this call. - Different legal case: For
[0.01,0.99], the independently calculated result is0.01,0.99. The complexity discussion uses O(n+k) average · O(n^2) worst · O(n+k) space. Verify the state invariant before reasoning about the operation count.
Transfer challenge: Change the independent fixture [0.9,0.1] in a legal way, then compare the result with your prediction. Explain how the same invariant applies to the separate visible challenge [0.42,0.32,0.23,0.52,0.25].
Connections
- Bucket Sort: When to Use It: roughly uniform numeric distributions
- Quickselect: Mental Model: Partition like quicksort but recurse only into the side containing the requested rank.
Reveal the Algorithm Atlas answer
0.23,0.25,0.32,0.42,0.52
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Challenge: Bucket Sort (courseId "algorithms", lessonId "algorithm.bucket-sort.challenge") Link: https://crabrix.com/learn/algorithms/algorithm.bucket-sort.challenge Goal: Values are distributed across buckets without a large concentration in one bucket. Values are decimals in [0,1). Distribute them into n buckets by floor(n*x), where n is the input length, sort each bucket, and return all values ascending. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=algorithms/algorithm.bucket-sort.challenge) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.