forked from BarrenSea/algori
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fffde4c
commit af9cdda
Showing
6 changed files
with
63 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/// Bubble Sort | ||
/// # Example | ||
/// ``` | ||
/// use algori::sorting::{bubble_sort,is_sorted}; | ||
/// let mut a = [2,3,1,34,15,8,0,7,4,3,21,4,6,7,4,2341,321,41231,312,62]; | ||
/// bubble_sort(&mut a,|a,b| a <= b); | ||
/// is_sorted(&mut a,|a,b| a<=b); | ||
/// ``` | ||
pub fn bubble_sort<T>(array: &mut [T], comparator: fn(&T, &T) -> bool) -> () { | ||
for point in (0..array.len()).rev() { | ||
for current_point in 0..point { | ||
if !comparator(&array[current_point],&array[current_point+1]) { | ||
array.swap(current_point,current_point + 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod bubble_sort_tests { | ||
use super::super::is_sorted; | ||
use super::bubble_sort; | ||
#[test] | ||
fn empty() -> () { | ||
let mut a: [i32; 0] = []; | ||
bubble_sort(&mut a, |a, b| a <= b); | ||
|
||
assert_eq!(is_sorted(&mut a, |a, b| a <= b), true); | ||
} | ||
|
||
#[test] | ||
fn one_element() -> () { | ||
let mut a: [i32; 1] = [1]; | ||
bubble_sort(&mut a, |a, b| a <= b); | ||
assert_eq!(is_sorted(&mut a, |a, b| a <= b), true); | ||
} | ||
#[test] | ||
fn positive() -> () { | ||
let mut a = [1, 123, 123, 12, 4234, 42, 1123, 123, 15112, 312]; | ||
bubble_sort(&mut a, |a, b| a >= b); | ||
assert_eq!(is_sorted(&mut a, |a, b| a >= b), true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod insertion_sort; | ||
mod selection_sort; | ||
mod utils; | ||
mod bubble_sort; | ||
|
||
pub use self::insertion_sort::*; | ||
pub use self::selection_sort::*; | ||
pub use self::utils::*; | ||
pub use self::bubble_sort::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters