sort_it/algorithms/
bubble_sort.rs

1use std::time::{ Instant, Duration };
2
3/// A trait providing the bubble sort method.
4pub trait BubbleSort<T: PartialEq + PartialOrd + Clone + Copy> {
5    /// The bubble sort algorithm.
6    ///
7    /// Sorts the `Vec` it is called on.
8    fn bubble_sort(&mut self);
9
10    /// The bubble sort algorithm but timed.
11    ///
12    /// Sorts the `Vec` it is called on and returns the `Duration` of the process.
13    fn bubble_sort_timed(&mut self) -> Duration;
14
15    /// The bubble sort algorithm but stepped.
16    ///
17    /// Sorts the `Vec` it is called on and returns a `Vec` containing each step of the process.
18    fn bubble_sort_stepped(&mut self) -> Vec<Vec<T>>;
19
20    /// The bubble sort algorithm but stepped _and_ timed.
21    ///
22    /// Sorts the `Vec` it is called on and returns a `Vec` containing each step of the process,
23    /// including the `Duration` of the entire process.
24    fn bubble_sort_stepped_and_timed(&mut self) -> (Vec<Vec<T>>, Duration);
25}
26
27/// The trait implementation of the bubble sort algorithm.
28impl<T> BubbleSort<T> for Vec<T> 
29    where T: PartialEq + PartialOrd + Clone + Copy,
30{
31    fn bubble_sort(&mut self) {
32        if self.len() <= 1 {
33            return;
34        }
35
36        let mut is_sorted = false;
37        while !is_sorted {
38            is_sorted = true;
39            for i in 1..self.len() {
40                if self[i] < self[i-1] {
41                    is_sorted = false;
42                    self.swap(i, i-1);
43                }
44            }
45        }
46    }
47
48    fn bubble_sort_timed(&mut self) -> Duration {
49        let time = Instant::now();
50
51        if self.len() <= 1 {
52            return time.elapsed();
53        }
54
55        let mut is_sorted = false;
56        while !is_sorted {
57            is_sorted = true;
58            for i in 1..self.len() {
59                if self[i] < self[i-1] {
60                    is_sorted = false;
61                    self.swap(i, i-1);
62                }
63            }
64        }
65
66        return time.elapsed();
67    }
68
69    fn bubble_sort_stepped(&mut self) -> Vec<Vec<T>> {
70        let mut steps = vec![self.clone()];
71
72        if self.len() <= 1 {
73            return steps;
74        }
75
76        let mut is_sorted = false;
77        while !is_sorted {
78            is_sorted = true;
79            for i in 1..self.len() {
80                if self[i] < self[i-1] {
81                    is_sorted = false;
82                    self.swap(i, i-1);
83                    steps.push(self.clone());
84                }
85            }
86        }
87
88        return steps;
89    }
90
91    fn bubble_sort_stepped_and_timed(&mut self) -> (Vec<Vec<T>>, Duration) {
92        let time = Instant::now();
93
94        let mut steps = vec![self.clone()];
95
96        if self.len() <= 1 {
97            return (steps, time.elapsed());
98        }
99
100        let mut is_sorted = false;
101        while !is_sorted {
102            is_sorted = true;
103            for i in 1..self.len() {
104                if self[i] < self[i-1] {
105                    is_sorted = false;
106                    self.swap(i, i-1);
107                    steps.push(self.clone());
108                }
109            }
110        }
111
112        (steps, time.elapsed())
113    }
114}
115
116/// The bubble sort algorithm.
117///
118/// Sorts the given `Vec` and returns the result.
119pub fn bubble_sort<T>(mut arr: Vec<T>) -> Vec<T> 
120    where T: PartialEq + PartialOrd + Clone + Copy,
121{
122    if arr.len() <= 1 {
123        return arr;
124    }
125
126    let mut is_sorted = false;
127    while !is_sorted {
128        is_sorted = true;
129        for i in 1..arr.len() {
130            if arr[i] < arr[i-1] {
131                is_sorted = false;
132                arr.swap(i, i-1);
133            }
134        }
135    }
136
137    return arr;
138}
139
140/// The bubble sort algorithm but timed.
141///
142/// Sorts the given `Vec` and returns the result and the `Duration` of the process.
143pub fn bubble_sort_timed<T>(mut arr: Vec<T>) -> (Vec<T>, Duration)
144    where T: PartialEq + PartialOrd + Clone + Copy,
145{
146    let time = Instant::now();
147
148    if arr.len() <= 1 {
149        return (arr, time.elapsed());
150    }
151
152    let mut is_sorted = false;
153    while !is_sorted {
154        is_sorted = true;
155        for i in 1..arr.len() {
156            if arr[i] < arr[i-1] {
157                is_sorted = false;
158                arr.swap(i, i-1);
159            }
160        }
161    }
162
163    (arr, time.elapsed())
164}
165
166/// The bubble sort algorithm but stepped.
167///
168/// Sorts the given `Vec` and returns the result and a `Vec` containing each step of the process.
169pub fn bubble_sort_stepped<T>(mut arr: Vec<T>) -> (Vec<T>, Vec<Vec<T>>)
170    where T: PartialEq + PartialOrd + Clone + Copy,
171{
172    let mut steps = vec![arr.clone()];
173
174    if arr.len() <= 1 {
175        return (arr, steps);
176    }
177
178    let mut is_sorted = false;
179    while !is_sorted {
180        is_sorted = true;
181        for i in 1..arr.len() {
182            if arr[i] < arr[i-1] {
183                is_sorted = false;
184                arr.swap(i, i-1);
185                steps.push(arr.clone());
186            }
187        }
188    }
189
190    (arr, steps)
191}
192
193/// The bubble sort algorithm but stepped _and_ timed.
194///
195/// Sorts the given `Vec` and returns the result and a `Vec` containing each step of the process.
196pub fn bubble_sort_stepped_and_timed<T>(mut arr: Vec<T>) -> (Vec<T>, Vec<Vec<T>>, Duration)
197    where T: PartialEq + PartialOrd + Clone + Copy,
198{
199    let time = Instant::now();
200
201    let mut steps = vec![arr.clone()];
202
203    if arr.len() <= 1 {
204        return (arr, steps, time.elapsed());
205    }
206
207    let mut is_sorted = false;
208    while !is_sorted {
209        is_sorted = true;
210        for i in 1..arr.len() {
211            if arr[i] < arr[i-1] {
212                is_sorted = false;
213                arr.swap(i, i-1);
214                steps.push(arr.clone());
215            }
216        }
217    }
218
219    (arr, steps, time.elapsed())
220}
221