冒泡排序
use std::io::*;
fn main() {
let mut str_buf = String::new();
stdin().read_line(&mut str_buf).expect("failed to read");
str_buf.clear();
stdin().read_line(&mut str_buf).expect("failed to read");
let num_arr: Vec<i32> = str_buf
.trim()
.split_whitespace()
.into_iter()
.map(|s| s.parse().unwrap())
.collect();
let arr_inc = bubble_sort(&num_arr, |a, b| a > b);
arr_inc.iter().for_each(|e| print!("{} ", e));
println!();
let arr_dec = bubble_sort(&num_arr, |a, b| a < b);
arr_dec.iter().for_each(|e| print!("{} ", e));
println!();
}
fn bubble_sort(input: &Vec<i32>, cmp: fn(a: i32, b: i32) -> bool) -> Vec<i32> {
let mut sorting_arr = input.clone();
let n = sorting_arr.len();
for i in 0..n {
for j in 0..n-i-1 {
if cmp(sorting_arr[j], sorting_arr[j+1]) {
let tmp = sorting_arr[j];
sorting_arr[j] = sorting_arr[j+1];
sorting_arr[j+1] = tmp;
}
}
}
sorting_arr
}