Skip to main content

bubblesort_ifyer/
lib.rs

1/// Sorts stuff_to_sort using the bubblesort algorithom and returns it
2/// Behaviur with NaN and and infinity and negative 0 is undefined
3/// Behaviour when version != 1 && version != 255 && version != 254 is undefined
4/// Recursiveness decicides how efficent the program is with 0 being the most efficent. However currently recursiveness must be None.
5use sorted_ifyer::is_sorted;
6pub fn bubblesort<T: std::clone::Clone + std::cmp::PartialOrd>(stuff_to_sort: &[T]) -> Vec<T> {
7    let mut sorted_stuff = stuff_to_sort.to_vec();
8    if sorted_stuff.is_empty() {
9        return sorted_stuff;
10    }
11    let mut lowest_sorted = stuff_to_sort.len() - 1;
12    while !is_sorted(stuff_to_sort, lowest_sorted + 1, 0) {
13        for i in 1..=lowest_sorted {
14            if sorted_stuff[i - 1] > sorted_stuff[i] {
15                sorted_stuff.swap(i - 1, i);
16            }
17            if i == lowest_sorted {
18                lowest_sorted -= 1
19            }
20        }
21    }
22    sorted_stuff
23}