feat: première version
This commit is contained in:
parent
c5e21537e3
commit
3c37c5802c
1 changed files with 51 additions and 0 deletions
51
src/main.rs
Normal file
51
src/main.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
/* challenge de NaN ou il faut faire le tri d’un 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) }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue