diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5cf672f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,51 @@ +/* challenge de NaN ou il faut faire le tri d’un tableau + * Copyright (C) 2021 rick + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +fn tri(n: usize, arr: &mut [u32]) -> Result<(), &str> { + if arr.len() < n { + return Err("n supérieur à la taille du tableau."); + } + + let mut i: usize = 0; + let mut j: usize; + let mut tmp: u32; + + while i < n { + tmp = arr[i]; + j = i; + while j < n { + if tmp > arr[j] { + arr[i] = arr[j]; + arr[j] = tmp; + break; + } + j += 1; + } + + if j == n { + i += 1; + } + } + Ok(()) +} + +fn main() { + let mut test : [u32; 5] = [5, 2, 1, 4, 5]; + match tri(5, &mut test) { + Ok(_) => { println!("Tableau = {:?}", test) } + Err(e) => { println!("{}", e) } + } +}