sorting_algorithm/exchange_sort.rs
1/// Sorts a data set using Exchange Sort
2///
3/// Average time complexity: O(n<sup>2</sup>)
4///
5/// # Example
6///
7/// ```
8/// use sorting_algorithm::exchange_sort;
9///
10/// fn main() {
11/// let mut data = [3, 1, 2, 5, 4];
12///
13/// exchange_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 if data.len() <= 1 {
20 return;
21 }
22
23 let n = data.len();
24
25 for i in 0..(n-1) {
26 for j in (i+1)..n {
27 if data[i] > data[j] {
28 data.swap(i, j);
29 }
30 }
31 }
32}