1+ /// This function works only with integers
2+ /// For ordering float values use https://crates.io/crates/float-ord
13pub fn bubble_sort < T : Ord > ( arr : & mut [ T ] ) {
24 if arr. is_empty ( ) {
35 return ;
@@ -21,6 +23,8 @@ mod tests {
2123 use super :: * ;
2224 use crate :: sorting:: have_same_elements;
2325 use crate :: sorting:: is_sorted;
26+ use rand:: rng;
27+ use rand:: seq:: SliceRandom ;
2428
2529 #[ test]
2630 fn descending ( ) {
@@ -46,4 +50,34 @@ mod tests {
4650 bubble_sort ( & mut ve3) ;
4751 assert ! ( is_sorted( & ve3) && have_same_elements( & ve3, & cloned) ) ;
4852 }
53+ #[ test]
54+ fn duplicate ( ) {
55+ let mut ve3: Vec < usize > = vec ! [ 2 , 3 , 3 , 5 , 7 , 1 , 1 ] ;
56+ let cloned = ve3. clone ( ) ;
57+ bubble_sort ( & mut ve3) ;
58+ assert ! ( is_sorted( & ve3) && have_same_elements( & ve3, & cloned) ) ;
59+ }
60+ #[ test]
61+ fn negative ( ) {
62+ let mut ve3: Vec < i32 > = ( -10 ..-1 ) . rev ( ) . collect ( ) ;
63+ let cloned = ve3. clone ( ) ;
64+ bubble_sort ( & mut ve3) ;
65+ assert ! ( is_sorted( & ve3) && have_same_elements( & ve3, & cloned) ) ;
66+ }
67+ #[ test]
68+ fn withnull ( ) {
69+ let mut ve3 = vec ! [ 2 , 3 , -3 , 5 , 7 , 0 , 1 ] ;
70+ let cloned = ve3. clone ( ) ;
71+ bubble_sort ( & mut ve3) ;
72+ assert ! ( is_sorted( & ve3) && have_same_elements( & ve3, & cloned) ) ;
73+ }
74+ /// Takes much time
75+ #[ test]
76+ fn long ( ) {
77+ let mut ve3: Vec < i32 > = ( 10 ..39000 ) . collect ( ) ;
78+ ve3. shuffle ( & mut rng ( ) ) ;
79+ let cloned = ve3. clone ( ) ;
80+ bubble_sort ( & mut ve3) ;
81+ assert ! ( is_sorted( & ve3) && have_same_elements( & ve3, & cloned) ) ;
82+ }
4983}
0 commit comments