added bubble_sort, binary_search, linear_search
This commit is contained in:
commit
f895efce44
13 changed files with 305 additions and 0 deletions
51
src/bubble_sort.rs
Normal file
51
src/bubble_sort.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::bubble_sort;
|
||||
#[test]
|
||||
fn bubble_sort_already_sorted_input(){
|
||||
let mut arr = [1, 2, 3, 4, 5];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, [1, 2, 3, 4, 5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bubble_sort_reverse_sorted_input(){
|
||||
let mut arr = [5, 4, 3, 2, 1];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, [1, 2, 3, 4, 5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bubble_sort_input_w_duplicate_elems(){
|
||||
let mut arr = [3, 1, 4, 1, 5, 9, 2, 6, 5];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, [1, 1, 2, 3, 4, 5, 5, 6, 9]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bubble_sort_empty_input(){
|
||||
let mut arr: [usize; 0] = [];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, []);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bubble_sort_input_w_one_elem(){
|
||||
let mut arr = [42];
|
||||
bubble_sort(&mut arr);
|
||||
assert_eq!(arr, [42]);
|
||||
}
|
||||
}
|
||||
fn bubble_sort(arr: &mut [usize]) {
|
||||
let n = arr.len();
|
||||
for i in 0..n {
|
||||
for j in 0..n - 1 - i {
|
||||
if arr[j] > arr[j + 1] {
|
||||
let tmp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue