From 0f1909e59b25fc5635b184c3b2cd01217a52b3dc Mon Sep 17 00:00:00 2001 From: SEPURI-SAI-KRISHNA Date: Wed, 5 Aug 2026 12:26:52 +0530 Subject: [PATCH] fix: empty slice panics in bead_sort, bogo_sort, comb_sort and wave_sort --- src/sorting/bead_sort.rs | 29 +++++++++++++++++++++++++++++ src/sorting/bogo_sort.rs | 23 ++++++++++++++++------- src/sorting/comb_sort.rs | 22 ++++++++++++++++++++++ src/sorting/wave_sort.rs | 17 ++++++++++++++++- 4 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/sorting/bead_sort.rs b/src/sorting/bead_sort.rs index c8c4017942e..45394c4f23d 100644 --- a/src/sorting/bead_sort.rs +++ b/src/sorting/bead_sort.rs @@ -1,6 +1,11 @@ //Bead sort only works for sequences of non-negative integers. //https://en.wikipedia.org/wiki/Bead_sort pub fn bead_sort(a: &mut [usize]) { + // An empty slice has no maximum to seed the bead grid with. + if a.is_empty() { + return; + } + // Find the maximum element let mut max = a[0]; (1..a.len()).for_each(|i| { @@ -56,4 +61,28 @@ mod tests { bead_sort(&mut ve2); assert!(is_sorted(&ve2) && have_same_elements(&ve2, &cloned)); } + + #[test] + fn empty() { + let mut ve3: Vec = vec![]; + let cloned = ve3.clone(); + bead_sort(&mut ve3); + assert!(is_sorted(&ve3) && have_same_elements(&ve3, &cloned)); + } + + #[test] + fn one_element() { + let mut ve4: [usize; 1] = [4]; + let cloned = ve4; + bead_sort(&mut ve4); + assert!(is_sorted(&ve4) && have_same_elements(&ve4, &cloned)); + } + + #[test] + fn all_zeroes() { + let mut ve5: [usize; 3] = [0, 0, 0]; + let cloned = ve5; + bead_sort(&mut ve5); + assert!(is_sorted(&ve5) && have_same_elements(&ve5, &cloned)); + } } diff --git a/src/sorting/bogo_sort.rs b/src/sorting/bogo_sort.rs index 572a9dd6584..96818ec66aa 100644 --- a/src/sorting/bogo_sort.rs +++ b/src/sorting/bogo_sort.rs @@ -4,13 +4,8 @@ use std::time::{SystemTime, UNIX_EPOCH}; const DEFAULT: u64 = 4294967296; fn is_sorted(arr: &[T], len: usize) -> bool { - for i in 0..len - 1 { - if arr[i] > arr[i + 1] { - return false; - } - } - - true + // `windows` yields nothing below two elements, where `0..len - 1` underflowed. + arr[..len].windows(2).all(|pair| pair[0] <= pair[1]) } #[cfg(target_pointer_width = "64")] @@ -70,4 +65,18 @@ mod tests { assert!(arr[i] <= arr[i + 1]); } } + + #[test] + fn empty() { + let mut arr: Vec = vec![]; + bogo_sort(&mut arr); + assert!(arr.is_empty()); + } + + #[test] + fn one_element() { + let mut arr = [7]; + bogo_sort(&mut arr); + assert_eq!(&arr, &[7]); + } } diff --git a/src/sorting/comb_sort.rs b/src/sorting/comb_sort.rs index d84522ce2ee..c962d8a3c82 100644 --- a/src/sorting/comb_sort.rs +++ b/src/sorting/comb_sort.rs @@ -1,4 +1,10 @@ pub fn comb_sort(arr: &mut [T]) { + // `gap` is clamped to at least 1 below, so `arr.len() - gap` would underflow + // on an empty slice. + if arr.len() < 2 { + return; + } + let mut gap = arr.len(); let shrink = 1.3; let mut sorted = false; @@ -51,4 +57,20 @@ mod tests { comb_sort(&mut ve3); assert!(is_sorted(&ve3) && have_same_elements(&ve3, &cloned)); } + + #[test] + fn empty() { + let mut ve4: Vec = vec![]; + let cloned = ve4.clone(); + comb_sort(&mut ve4); + assert!(is_sorted(&ve4) && have_same_elements(&ve4, &cloned)); + } + + #[test] + fn one_element() { + let mut ve5 = vec![3]; + let cloned = ve5.clone(); + comb_sort(&mut ve5); + assert!(is_sorted(&ve5) && have_same_elements(&ve5, &cloned)); + } } diff --git a/src/sorting/wave_sort.rs b/src/sorting/wave_sort.rs index 06e2e3dec97..c6c619e6312 100644 --- a/src/sorting/wave_sort.rs +++ b/src/sorting/wave_sort.rs @@ -19,7 +19,8 @@ pub fn wave_sort(arr: &mut [T]) { let n = arr.len(); arr.sort(); - for i in (0..n - 1).step_by(2) { + // `saturating_sub` keeps an empty slice from underflowing to `usize::MAX`. + for i in (0..n.saturating_sub(1)).step_by(2) { arr.swap(i, i + 1); } } @@ -67,4 +68,18 @@ mod tests { let expected = vec![10, 5, 20, 15, 25]; assert_eq!(&array, &expected); } + + #[test] + fn empty() { + let mut array: Vec = vec![]; + wave_sort(&mut array); + assert!(array.is_empty()); + } + + #[test] + fn one_element() { + let mut array = vec![42]; + wave_sort(&mut array); + assert_eq!(&array, &[42]); + } }