1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

feat(solutions): add sorting-algorithms/rust/insertion-sort

This commit is contained in:
Divlo 2022-03-08 10:02:48 +01:00
parent 88e466cb11
commit c502c17e7c
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
2 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# sorting-algorithms/rust/insertion-sort
Created by [@Divlo](https://github.com/Divlo) on 8 March 2022.

View File

@ -0,0 +1,25 @@
use std::io::{self, BufRead};
fn main() {
let mut numbers: Vec<i64> = Vec::new();
let stdin = io::stdin();
for line in stdin.lock().lines().skip(1) {
let line = line.unwrap();
let number: i64 = line.trim().parse().unwrap();
numbers.push(number);
}
insertion_sort(&mut numbers);
for number in numbers {
println!("{}", number);
}
}
pub fn insertion_sort<T: Ord>(array: &mut [T]) {
for index in 1..array.len() {
let mut index2 = index;
while index2 > 0 && array[index2] < array[index2 - 1] {
array.swap(index2, index2 - 1);
index2 -= 1;
}
}
}