bob_sort/lib.rs
1pub mod utils {
2
3 use std::vec::Vec;
4
5 // Sort Variant
6 pub enum TypeSort {
7 Bubble,
8 NoSort,
9 }
10
11 // Sort Type
12 pub struct Sort {
13 pub type_sort: TypeSort,
14 pub vector: Vec<i128>,
15 }
16
17 // Method
18 impl Sort {
19 // Create new Sort-Type variable
20 pub fn new(type_sort: TypeSort, vector: Vec<i128>) -> Sort {
21 Sort { type_sort, vector }
22 }
23
24 // Sort function
25 pub fn sort(&mut self) {
26 // Other type of sorting is not implemented yet
27 if let TypeSort::Bubble = self.type_sort {
28 // First iteration
29 for _ in 0..self.vector.len() {
30 // Second iteration
31 for i in 0..self.vector.len() - 1 {
32 // Compare if vec[i] is less than vec[i + 1]
33 if self.vector[i] > self.vector[i + 1] {
34 // If true, swap the items inside
35 self.vector.swap(i, i + 1);
36 }
37 }
38 }
39 }
40 }
41 } // Sort Implementation
42}