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
use Sortable;
/// Cocktail sorts in-place, stable, in ascending order a mutable ref slice of type T: Sortable
///
/// Cocktail is a variant of bubble sort. It continuously loops over elements in slice collection, swapping elements
/// if they are out of order. If no swaps occur in a loop then the sort is complete.
/// Each iteration swaps order of the iteration from left to right and from comparing smallest to largest, such that
/// the largest and smallest elements are bubbled up and down the list bidirectionally.
/// It aims to solve the rabbit and turtle problem of standard bubble sort where values that
/// need to move to the beginning of the list require many swaps to get there.
///
/// # Examples
///
/// ```
/// use rust_sort::cocktail_sort::sort;
///
/// let mut arr = [3, 2, 1, 7, 9, 4, 1, 2];
/// sort(&mut arr);
/// assert_eq!(arr, [1, 1, 2, 2, 3, 4, 7, 9]);
///
/// ```