1
0
Fork 0

feat: première version

This commit is contained in:
rick 2021-07-24 03:43:02 +02:00
parent c5e21537e3
commit 3c37c5802c
Signed by: Rick
GPG key ID: 2B593F087240EE99

51
src/main.rs Normal file
View file

@ -0,0 +1,51 @@
/* 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() {
let mut test : [u32; 5] = [5, 2, 1, 4, 5];
match tri(5, &mut test) {
Ok(_) => { println!("Tableau = {:?}", test) }
Err(e) => { println!("{}", e) }
}
}