-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertion_sort.rs
78 lines (63 loc) · 2.06 KB
/
insertion_sort.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
pub fn insertion_sort<T: Ord + Copy>(arr: &mut [T]) {
if arr.is_empty() {
return;
}
for index in 1..arr.len() {
let current_item: T = arr[index];
let mut insertion_index: usize = index;
while insertion_index > 0 && arr[insertion_index - 1] > current_item {
arr[insertion_index] = arr[insertion_index - 1];
insertion_index -= 1;
}
arr[insertion_index] = current_item;
}
}
// alternatively we can use 'rotate_right' function from rust std
/*
the main idea is only to find the index at which we need to put 'current_item'
and let 'rotate_right' to do all the swaps
we find the correct position for 'current_item' without making any swaps
and what 'rotate_right(1)' do is:
shift every item one position right and the rightmost items goes to first position
for example:
arr = [1, 2, 3]
index = 2
insertion_index = 0
arr[insertion_index..=index].rotate_right(1);
arr = [3, 1, 2]
*/
pub fn insertion_sort_with_rotate_right<T: Ord + Copy>(arr: &mut [T]) {
if arr.is_empty() {
return;
}
for index in 1..arr.len() {
let current_item: T = arr[index];
let mut insertion_index: usize = index;
while insertion_index > 0 && arr[insertion_index - 1] > current_item {
insertion_index -= 1;
}
arr[insertion_index..=index].rotate_right(1);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty() {
let mut empty_vec: Vec<i32> = Vec::new();
insertion_sort(&mut empty_vec);
assert!(empty_vec.is_empty());
}
#[test]
fn sort() {
let mut arr: Vec<i32> = vec![4, -3, 7, 0, 10, -6, 7, 1];
insertion_sort(&mut arr);
assert_eq!(arr, vec![-6, -3, 0, 1, 4, 7, 7, 10]);
}
#[test]
fn sort_with_rotate_right() {
let mut arr: Vec<i32> = vec![4, -3, 7, 0, 10, -6, 7, 1];
insertion_sort_with_rotate_right(&mut arr);
assert_eq!(arr, vec![-6, -3, 0, 1, 4, 7, 7, 10]);
}
}