Challenge - 5 Problems
Bubble Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bubble Sort on a small array
What is the output of the following Rust code after performing bubble sort on the array?
DSA Rust
fn bubble_sort(arr: &mut [i32]) {
let n = arr.len();
for i in 0..n {
for j in 0..n - i - 1 {
if arr[j] > arr[j + 1] {
arr.swap(j, j + 1);
}
}
}
}
fn main() {
let mut data = [4, 2, 5, 1];
bubble_sort(&mut data);
println!("{:?}", data);
}Attempts:
2 left
💡 Hint
Bubble sort repeatedly swaps adjacent elements if they are in the wrong order.
✗ Incorrect
The bubble sort algorithm compares adjacent elements and swaps them if the left one is greater than the right one. After all passes, the array is sorted in ascending order.
🧠 Conceptual
intermediateNumber of passes in Bubble Sort
For an array of length n, how many passes does the bubble sort algorithm perform in the worst case?
Attempts:
2 left
💡 Hint
Each pass places the largest unsorted element at the end.
✗ Incorrect
Bubble sort performs n-1 passes because after each pass, the largest unsorted element is placed at its correct position, so only n-1 passes are needed to sort n elements.
🔧 Debug
advancedIdentify the bug in this Bubble Sort implementation
What error will this Rust bubble sort code cause when run?
DSA Rust
fn bubble_sort(arr: &mut [i32]) {
let n = arr.len();
for i in 0..n {
for j in 0..n - i {
if arr[j] > arr[j + 1] {
arr.swap(j, j + 1);
}
}
}
}Attempts:
2 left
💡 Hint
Check the inner loop range and the index used for swapping.
✗ Incorrect
The inner loop runs from 0 to n - i, but accessing arr[j + 1] when j = n - i - 1 causes an index out of bounds panic because arr[j + 1] is out of range.
❓ Predict Output
advancedOutput after partial bubble sort passes
What is the state of the array after the first pass of bubble sort on [3, 1, 4, 2]?
DSA Rust
fn bubble_sort_pass(arr: &mut [i32]) {
let n = arr.len();
for j in 0..n - 1 {
if arr[j] > arr[j + 1] {
arr.swap(j, j + 1);
}
}
}
fn main() {
let mut data = [3, 1, 4, 2];
bubble_sort_pass(&mut data);
println!("{:?}", data);
}Attempts:
2 left
💡 Hint
One pass moves the largest element to the end.
✗ Incorrect
During the first pass, adjacent elements are compared and swapped if needed. The largest element 4 moves to the end, and the array becomes [1, 3, 2, 4].
🚀 Application
expertOptimizing Bubble Sort to stop early
Which code snippet correctly implements an optimized bubble sort that stops if no swaps occur during a pass?
Attempts:
2 left
💡 Hint
Track if any swap happened during a pass and break if none.
✗ Incorrect
Option B correctly uses a boolean flag 'swapped' to detect if any swaps occurred during a pass. If no swaps happen, the array is sorted and the loop breaks early. Option B is also correct logically but does not reduce the range of inner loop as passes progress, making it less efficient. Option B sorts in descending order, not ascending. Option B never sets swapped to true, so it never breaks early.
