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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::time::{ Instant, Duration };

/// A trait providing the selection sort algorithm.
pub trait SelectionSort<T: PartialEq + PartialOrd + Clone + Copy> {
    /// The selection sort algorithm.
    ///
    /// Sorts the `Vec` it is called on.
    fn selection_sort(&mut self);

    /// The selection sort algorithm but timed.
    ///
    /// Sorts the `Vec` it is called on and returns the `Duration` of the process.
    fn selection_sort_timed(&mut self) -> Duration;

    /// The selection sort algorithm but stepped.
    ///
    /// Sorts the `Vec` it is called on and returns a `Vec` containing each step of the process.
    fn selection_sort_stepped(&mut self) -> Vec<(Vec<T>, Vec<T>)>;

    /// The selection sort algorithm but stepped _and_ timed.
    ///
    /// Sorts the `Vec` it is called on and returns a `Vec` containing each step of the process,
    /// including the `Duration` of the entire process.
    fn selection_sort_stepped_and_timed(&mut self) -> (Vec<(Vec<T>, Vec<T>)>, Duration);
}

/// The trait implementation of the selection sort algorithm.
impl<T> SelectionSort<T> for Vec<T>
    where T: PartialEq + PartialOrd + Clone + Copy,
{
    fn selection_sort(&mut self) {
        if self.len() <= 1 {
            return;
        }

        let mut sorted = vec![];

        while self.len() > 0 {
            let mut min_idx = 0;
            for i in 0..self.len() {
                if self[min_idx] > self[i] {
                    min_idx = i;
                }
            }
            sorted.push(self.remove(min_idx));
        }

        *self = sorted;
    }

    fn selection_sort_timed(&mut self) -> Duration {
        let time = Instant::now();

        if self.len() <= 1 {
            return time.elapsed();
        }

        let mut sorted = vec![];

        while self.len() > 0 {
            let mut min_idx = 0;
            for i in 0..self.len() {
                if self[min_idx] > self[i] {
                    min_idx = i;
                }
            }
            sorted.push(self.remove(min_idx));
        }

        *self = sorted;

        return time.elapsed();
    }

    fn selection_sort_stepped(&mut self) -> Vec<(Vec<T>, Vec<T>)> {
        let mut steps = vec![(self.clone(), vec![])];

        if self.len() <= 1 {
            return steps;
        }

        let mut sorted = vec![];

        while self.len() > 0 {
            let mut min_idx = 0;
            for i in 0..self.len() {
                if self[min_idx] > self[i] {
                    min_idx = i;
                }
            }
            sorted.push(self.remove(min_idx));
            steps.push((self.clone(), sorted.clone()));
        }

        *self = sorted;

        return steps;
    }

    fn selection_sort_stepped_and_timed(&mut self) -> (Vec<(Vec<T>, Vec<T>)>, Duration) {
        let time = Instant::now();

        let mut steps = vec![(self.clone(), vec![])];

        if self.len() <= 1 {
            return (steps, time.elapsed());
        }

        let mut sorted = vec![];

        while self.len() > 0 {
            let mut min_idx = 0;
            for i in 0..self.len() {
                if self[min_idx] > self[i] {
                    min_idx = i;
                }
            }
            sorted.push(self.remove(min_idx));
            steps.push((self.clone(), sorted.clone()));
        }

        *self = sorted;

        (steps, time.elapsed())
    }
}

/// The selection sort algorithm.
///
/// Sorts the given `Vec` and returns the result.
pub fn selection_sort<T>(mut arr: Vec<T>) -> Vec<T>
    where T: PartialEq + PartialOrd + Clone + Copy,
{
    if arr.len() <= 1 {
        return arr;
    }

    let mut sorted = vec![];

    while arr.len() > 0 {
        let mut min_idx = 0;
        for i in 0..arr.len() {
            if arr[min_idx] > arr[i] {
                min_idx = i;
            }
        }
        sorted.push(arr.remove(min_idx));
    }

    return sorted;
}

/// The selection sort algorithm but timed.
///
/// Sorts the given `Vec` and returns the result and the `Duration` of the entire process.
pub fn selection_sort_timed<T>(mut arr: Vec<T>) -> (Vec<T>, Duration)
    where T: PartialEq + PartialOrd + Clone + Copy,
{
    let time = Instant::now();

    if arr.len() <= 1 {
        return (arr, time.elapsed());
    }

    let mut sorted = vec![];

    while arr.len() > 0 {
        let mut min_idx = 0;
        for i in 0..arr.len() {
            if arr[min_idx] > arr[i] {
                min_idx = i;
            }
        }
        sorted.push(arr.remove(min_idx));
    }

    (sorted, time.elapsed())
}

/// The selection sort algorithm but stepped.
///
/// Sorts the given `Vec` and returns the result and a `Vec` containing the steps of the 
/// process as a tuple of the unsorted and sorted array.
pub fn selection_sort_stepped<T>(mut arr: Vec<T>) -> (Vec<T>, Vec<(Vec<T>, Vec<T>)>)
    where T: PartialEq + PartialOrd + Clone + Copy,
{
    let mut steps = vec![(arr.clone(), vec![])];

    if arr.len() <= 1 {
        return (arr, steps);
    }

    let mut sorted = vec![];

    while arr.len() > 0 {
        let mut min_idx = 0;
        for i in 0..arr.len() {
            if arr[min_idx] > arr[i] {
                min_idx = i;
            }
        }

        sorted.push(arr.remove(min_idx));
        steps.push((arr.clone(), sorted.clone()));
    }

    (sorted, steps)
}

/// The selection sort algorithm but stepped _and_ timed.
///
/// Sorts the given `Vec` and returns the result and a `Vec` containing the steps of the 
/// process as a tuple of the unsorted and sorted array, including the `Duration` of the 
/// entire process.
pub fn selection_sort_stepped_and_timed<T>(mut arr: Vec<T>) -> (Vec<T>, Vec<(Vec<T>, Vec<T>)>, Duration)
    where T: PartialEq + PartialOrd + Clone + Copy,
{
    let time = Instant::now();

    let mut steps = vec![(arr.clone(), vec![])];

    if arr.len() <= 1 {
        return (arr, steps, time.elapsed());
    }

    let mut sorted = vec![];

    while arr.len() > 0 {
        let mut min_idx = 0;
        for i in 0..arr.len() {
            if arr[min_idx] > arr[i] {
                min_idx = i;
            }
        }

        sorted.push(arr.remove(min_idx));
        steps.push((arr.clone(), sorted.clone()));
    }

    (sorted, steps, time.elapsed())
}