-
Notifications
You must be signed in to change notification settings - Fork 72
/
strand_sort.rs
124 lines (94 loc) · 3.35 KB
/
strand_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::collections::LinkedList;
pub fn strand_sort(ip: &mut LinkedList<i32>, op: &mut LinkedList<i32>) {
if ip.is_empty() {
return;
}
let mut sublist = LinkedList::new();
sublist.push_back(ip.pop_front().unwrap());
let mut iter = ip.split_off(0);
while let Some(val) = iter.pop_front() {
if val > *sublist.back().unwrap() {
sublist.push_back(val);
} else {
ip.push_back(val);
}
}
// Merge current sublist into output
let mut merged = LinkedList::new();
while !op.is_empty() || !sublist.is_empty() {
match (op.front(), sublist.front()) {
(Some(&op_val), Some(&sub_val)) if op_val <= sub_val => {
merged.push_back(op.pop_front().unwrap());
}
(_, Some(_)) => {
merged.push_back(sublist.pop_front().unwrap());
}
(Some(_), _) => {
merged.push_back(op.pop_front().unwrap());
}
(None, None) => break,
}
}
*op = merged;
strand_sort(ip, op);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_strand_sort() {
let mut ip: LinkedList<i32> = LinkedList::from([10, 5, 30, 40, 2, 4, 9]);
let mut op: LinkedList<i32> = LinkedList::new();
strand_sort(&mut ip, &mut op);
assert_eq!(op, LinkedList::from([2, 4, 5, 9, 10, 30, 40]));
}
#[test]
fn test_strand_sort_empty() {
let mut ip: LinkedList<i32> = LinkedList::new();
let mut op: LinkedList<i32> = LinkedList::new();
strand_sort(&mut ip, &mut op);
assert_eq!(op, LinkedList::new());
}
#[test]
fn test_strand_sort_single() {
let mut ip: LinkedList<i32> = LinkedList::from([1]);
let mut op: LinkedList<i32> = LinkedList::new();
strand_sort(&mut ip, &mut op);
assert_eq!(op, LinkedList::from([1]));
}
#[test]
fn test_strand_sort_negative() {
let mut ip: LinkedList<i32> = LinkedList::from([-1, -2, -3, -4, -5]);
let mut op: LinkedList<i32> = LinkedList::new();
strand_sort(&mut ip, &mut op);
assert_eq!(op, LinkedList::from([-5, -4, -3, -2, -1]));
}
#[test]
fn test_strand_sort_duplicates() {
let mut ip: LinkedList<i32> = LinkedList::from([1, 1, 1, 1, 1]);
let mut op: LinkedList<i32> = LinkedList::new();
strand_sort(&mut ip, &mut op);
assert_eq!(op, LinkedList::from([1, 1, 1, 1, 1]));
}
#[test]
fn test_strand_sort_error() {
let mut ip: LinkedList<i32> = LinkedList::from([1, 2, 3, 4, 5]);
let mut op: LinkedList<i32> = LinkedList::new();
strand_sort(&mut ip, &mut op);
assert_ne!(op, LinkedList::from([2, 1, 3, 4, 5]));
}
#[test]
fn test_strand_sort_big() {
let mut ip: LinkedList<i32> = LinkedList::from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
let mut op: LinkedList<i32> = LinkedList::new();
strand_sort(&mut ip, &mut op);
assert_eq!(op, LinkedList::from([1, 2, 3, 4, 5, 6, 7, 8, 9]));
}
#[test]
fn test_strand_sort_big_reverse() {
let mut ip: LinkedList<i32> = LinkedList::from([9, 8, 7, 6, 5, 4, 3, 2, 1]);
let mut op: LinkedList<i32> = LinkedList::new();
strand_sort(&mut ip, &mut op);
assert_eq!(op, LinkedList::from([1, 2, 3, 4, 5, 6, 7, 8, 9]));
}
}