Bubble Sort is one of the simplest sorting algorithms that every beginner should learn. It works by repeatedly comparing adjacent elements in an array and swapping them if they are in the wrong order. The largest elements “bubble up” to the end of the array with each pass, which is why it is called Bubble Sort. Though it is not the most efficient algorithm for large datasets, learning Bubble Sort helps beginners understand the basics of sorting, iteration, and algorithmic thinking.
with hands-on learning.
get the skills and confidence to land your next move.
In Rust, Bubble Sort is a great exercise for understanding ownership, mutability, and loops. Since Rust emphasizes safety and performance, implementing Bubble Sort allows learners to practice writing efficient, safe code while also exploring basic algorithmic patterns. This knowledge forms a strong foundation for moving on to more advanced sorting algorithms in Rust.
Program 1: Bubble Sort Using a Simple Loop
This first program demonstrates a basic Bubble Sort implementation using nested loops. The array is predefined with integer values.
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 numbers = [64, 34, 25, 12, 22, 11, 90];
println!("Original array: {:?}", numbers);
bubble_sort(&mut numbers);
println!("Sorted array: {:?}", numbers);
}This program uses nested loops where the outer loop controls passes, and the inner loop performs comparisons. Swapping is done using Rust’s swap method, which safely exchanges values. Beginners can see how the largest numbers move toward the end with each pass.
Program 2: Bubble Sort with Early Exit Optimization
This version introduces a flag to check if the array is already sorted, reducing unnecessary passes.
fn bubble_sort_optimized(arr: &mut [i32]) {
let n = arr.len();
for i in 0..n {
let mut swapped = false;
for j in 0..n-i-1 {
if arr[j] > arr[j+1] {
arr.swap(j, j+1);
swapped = true;
}
}
if !swapped {
break;
}
}
}
fn main() {
let mut numbers = [5, 1, 4, 2, 8];
println!("Original array: {:?}", numbers);
bubble_sort_optimized(&mut numbers);
println!("Sorted array: {:?}", numbers);
}The swapped flag allows the algorithm to stop early if the array is already sorted. This optimization is useful for nearly sorted arrays and shows beginners how to make simple algorithms more efficient.
Program 3: Bubble Sort Using Recursion
Recursion can also implement Bubble Sort. Here, the function repeatedly processes the array until it is sorted.
fn bubble_sort_recursive(arr: &mut [i32], n: usize) {
if n == 1 {
return;
}
for i in 0..n - 1 {
if arr[i] > arr[i + 1] {
arr.swap(i, i + 1);
}
}
bubble_sort_recursive(arr, n - 1);
}
fn main() {
let mut numbers = [10, 3, 15, 7, 8];
println!("Original array: {:?}", numbers);
let size = numbers.len();
bubble_sort_recursive(&mut numbers, size);
println!("Sorted array: {:?}", numbers);
}This program shows how recursion can replace loops. Beginners learn how to break problems into smaller parts, a fundamental concept in Rust and programming in general.
Program 4: Bubble Sort for Floating-Point Numbers
Bubble Sort is not limited to integers. This example sorts floating-point numbers.
fn bubble_sort_floats(arr: &mut [f64]) {
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 numbers = [3.5, 2.1, 4.7, 1.0, 5.3];
println!("Original array: {:?}", numbers);
bubble_sort_floats(&mut numbers);
println!("Sorted array: {:?}", numbers);
}This program highlights Rust’s type system and shows that Bubble Sort can be applied to any comparable type, helping beginners understand generics and type-specific operations.
Program 5: Bubble Sort Using Generics
Rust’s generics allow us to implement a reusable Bubble Sort for any type that supports comparison.
fn bubble_sort_generic<T: Ord>(arr: &mut [T]) {
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 int_arr = [9, 4, 6, 2, 8];
let mut char_arr = ['d', 'a', 'c', 'b'];
println!("Original integer array: {:?}", int_arr);
bubble_sort_generic(&mut int_arr);
println!("Sorted integer array: {:?}", int_arr);
println!("Original char array: {:?}", char_arr);
bubble_sort_generic(&mut char_arr);
println!("Sorted char array: {:?}", char_arr);
}Using generics teaches beginners how to write flexible, reusable code. Bubble Sort logic stays the same, but now it works for multiple types without rewriting functions.
Frequently Asked Questions (FAQ)
This section clears common doubts beginners often have about Bubble Sort in Rust.
Q1: Why learn Bubble Sort in Rust?
Bubble Sort helps beginners understand loops, swapping, and algorithmic thinking while practicing Rust’s ownership and mutability concepts.
Q2: Is Bubble Sort efficient for large datasets?
No, Bubble Sort is simple but not efficient for large arrays. For bigger datasets, Rust programmers often use Quick Sort or Merge Sort.
Q3: Can Bubble Sort work with strings or other types?
Yes, Bubble Sort can work with any type that implements the Ord trait, including strings, characters, and floating-point numbers.
Q4: What is the main idea behind Bubble Sort?
The algorithm repeatedly swaps adjacent elements if they are in the wrong order, gradually moving the largest elements to the end of the array.
Q5: How can beginners optimize Bubble Sort?
Using an early-exit flag or applying recursion carefully can make Bubble Sort slightly faster, especially for nearly sorted arrays.
Conclusion
Bubble Sort is a beginner-friendly algorithm that teaches fundamental programming concepts such as loops, swapping, and comparisons. Implementing it in Rust helps learners understand key features like mutability, ownership, generics, and recursion. While Bubble Sort is not ideal for large datasets, mastering it builds a solid foundation for more advanced sorting techniques and problem-solving in Rust. Practicing multiple versions and types of Bubble Sort allows beginners to explore Rust’s power while reinforcing algorithmic thinking.




