fork(1) download
  1. use std::cmp::Ordering;
  2. use std::fmt::Debug;
  3. trait Bubble<T> {
  4. fn bsort_by<F>(&mut self, compare: F) where F: Fn(&T, &T) -> Ordering;
  5. fn bsort(&mut self) where T: Ord;
  6. }
  7. impl<T> Bubble<T> for Vec<T> {
  8. fn bsort_by<F>(&mut self, compare: F) where F: Fn(&T, &T) -> Ordering {
  9. for i in 0..self.len() {
  10. for j in (i + 1..self.len()).rev() {
  11. if compare(&self[j - 1], &self[j]) == Ordering::Greater {
  12. self.swap(j - 1, j);
  13. }
  14. }
  15. }
  16. }
  17. fn bsort(&mut self) where T: Ord {
  18. self.bsort_by(|a, b| a.cmp(b));
  19. }
  20. }
  21. fn p<T: Debug>(x: &T) {
  22. println!("{:?}", x);
  23. }
  24. fn f432<T: Debug + Ord>(v: &mut Vec<T>) {
  25. p(v); v.bsort(); p(v);
  26. }
  27. fn main() {
  28. f432(&mut vec![3, 2, 1, 0]);
  29. f432(&mut vec!['z', 'y', 'x', 'w']);
  30. }
Success #stdin #stdout 0s 11032KB
stdin
Standard input is empty
stdout
[3, 2, 1, 0]
[0, 1, 2, 3]
['z', 'y', 'x', 'w']
['w', 'x', 'y', 'z']