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
struct GapSequence {
    gap: usize,
}

impl GapSequence {
    fn new(n: usize) -> Self {
        Self { gap: n }
    }
}

impl Iterator for GapSequence {
    type Item = usize;

    fn next(&mut self) -> Option<usize> {
        self.gap /= 2;

        if self.gap > 0 {
            Some(self.gap)
        } else {
            None
        }
    }
}

pub fn shell_sort<T: PartialOrd>(s: &mut [T]) {
    let len = s.len();
    let gaps = GapSequence::new(len);

    for gap in gaps {
        for i in gap..len {
            let mut j = i;

            while j >= gap && s[j - gap] > s[j] {
                s.swap(j - gap, j);

                j -= gap;
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn correct_gap_sequence() {
        let gaps: Vec<_> = GapSequence::new(10).collect();
        assert_eq!(gaps, &[5, 2, 1]);
    }

}