mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
feat(solutions): add sorting-algorithms/rust/bubble-sort
This commit is contained in:
@ -0,0 +1,3 @@
|
||||
# sorting-algorithms/rust/bubble-sort
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 8 March 2022.
|
@ -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);
|
||||
}
|
||||
bubble_sort(&mut numbers);
|
||||
for number in numbers {
|
||||
println!("{}", number);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bubble_sort<T: Ord>(array: &mut [T]) {
|
||||
for index1 in 0..array.len() {
|
||||
for index2 in 0..array.len() - 1 - index1 {
|
||||
if array[index2] > array[index2 + 1] {
|
||||
array.swap(index2, index2 + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user