1
0
Fork 0
tri-rust/src/main.rs
2021-07-24 03:45:42 +02:00

55 lines
1.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* challenge de NaN ou il faut faire le tri dun tableau
* Copyright (C) 2021 rick <rick@gnous.eu>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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() {
// TODO faire des meilleurs test
let mut test : [u32; 5] = [5, 2, 1, 4, 5];
match tri(5, &mut test) {
Ok(_) => { println!("Tableau = {:?}", test) }
Err(e) => { println!("{}", e) }
}
}