sorting_algorithm/
gnome_sort.rs

1/// Sorts a data set using Gnome Sort
2///
3/// Average time complexity: O(n<sup>2</sup>)
4///
5/// # Examples
6///
7/// ```
8/// use sorting_algorithm::gnome_sort;
9///
10/// fn main() {
11///     let mut data = [3, 1, 2, 5, 4];
12///     
13///     gnome_sort::sort(&mut data);
14///
15///     assert_eq!(data, [1, 2, 3, 4, 5]);
16/// }
17/// ```
18pub fn sort<T: Ord>(data: &mut [T]) {
19    let len = data.len();
20
21    let mut i = 0;
22
23    while i < len {
24        if i == 0 {
25            i += 1;
26        }
27
28        if data[i] >= data[i - 1] {
29            i += 1;
30        } else {
31            data.swap(i, i - 1);
32            i -= 1;
33        }
34    }
35}