Skip to content

Commit

Permalink
增加冒泡排序
Browse files Browse the repository at this point in the history
  • Loading branch information
donjuanplatinum committed Jun 9, 2024
1 parent fffde4c commit af9cdda
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
19 changes: 14 additions & 5 deletions doc/sorting/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ math: true

+ 插入排序
+ 选择排序

| 排序 | 是否稳定 | 最坏情况时间 | 平均/期望时间 |
|----------|----------|-------------------|-------------------|
+ 冒泡排序
| 排序 | 是否稳定 | 最坏情况时间 | 平均/期望时间 |
|----------|----------|--------------------|--------------------|
| 插入排序 | 稳定 | $ \Theta( n ^ 2) $ | $ \Theta( n ^ 2) $ |
| 选择排序 | 不稳定 | $ \Theta( n ^ 2) $ | $ \Theta( n ^ 2) $ |
| 冒泡排序 | 稳定 | $ O( n ^ 2 ) $ | $ O( n ^ 2 ) $ |



## 插入排序
插入排序将元素插入到已排序序列的正确位置 并扩展已排序序列的边界
从第 **0** 个元素作为有序序列开始, 插入排序将元素 **插入****已排序序列** 的正确位置 并扩展已排序序列的边界

[![Insertion_sort](./insertion_sort.svg)](./insertion_sort.svg)

## 选择排序
选择排序按照指定的闭包每次找到第i最符合闭包条件的元素并放到i上
选择排序按照指定的 **闭包** 每次找到第i最符合闭包条件的元素 并放到 **i**

[![Selection_Sort](./selection_sort.svg)](./selection_sort.svg)


## 冒泡排序
比较相邻的元素 如果 **顺序不一样** 则交换

当第 **1** 次循环结束后 ,最后的元素将成为 **最大** 的元素, 并收缩右边界

[![Bubble_Sort](./bubble_sort.gif)](./bubble_sort.gif)

Binary file added doc/sorting/bubble_sort.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions src/sorting/bubble_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/// Bubble Sort
/// # Example
/// ```
/// use algori::sorting::{bubble_sort,is_sorted};
/// let mut a = [2,3,1,34,15,8,0,7,4,3,21,4,6,7,4,2341,321,41231,312,62];
/// bubble_sort(&mut a,|a,b| a <= b);
/// is_sorted(&mut a,|a,b| a<=b);
/// ```
pub fn bubble_sort<T>(array: &mut [T], comparator: fn(&T, &T) -> bool) -> () {
for point in (0..array.len()).rev() {
for current_point in 0..point {
if !comparator(&array[current_point],&array[current_point+1]) {
array.swap(current_point,current_point + 1);
}
}
}
}

#[cfg(test)]
mod bubble_sort_tests {
use super::super::is_sorted;
use super::bubble_sort;
#[test]
fn empty() -> () {
let mut a: [i32; 0] = [];
bubble_sort(&mut a, |a, b| a <= b);

assert_eq!(is_sorted(&mut a, |a, b| a <= b), true);
}

#[test]
fn one_element() -> () {
let mut a: [i32; 1] = [1];
bubble_sort(&mut a, |a, b| a <= b);
assert_eq!(is_sorted(&mut a, |a, b| a <= b), true);
}
#[test]
fn positive() -> () {
let mut a = [1, 123, 123, 12, 4234, 42, 1123, 123, 15112, 312];
bubble_sort(&mut a, |a, b| a >= b);
assert_eq!(is_sorted(&mut a, |a, b| a >= b), true);
}
}
2 changes: 1 addition & 1 deletion src/sorting/insertion_sort.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// # Insertion Sort
/// Insertion Sort
/// # Example
/// ```
/// use algori::sorting::{insertion_sort,is_sorted};
Expand Down
2 changes: 2 additions & 0 deletions src/sorting/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod insertion_sort;
mod selection_sort;
mod utils;
mod bubble_sort;

pub use self::insertion_sort::*;
pub use self::selection_sort::*;
pub use self::utils::*;
pub use self::bubble_sort::*;
6 changes: 3 additions & 3 deletions src/sorting/selection_sort.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// # Selection Sort
/// Selection Sort
/// # Example
/// ```
/// use algori::sorting::{selection_sort,is_sorted};
Expand Down Expand Up @@ -39,7 +39,7 @@ mod selection_sort_tests {
#[test]
fn positive() -> () {
let mut a = [1, 123, 123, 12, 4234, 42, 1123, 123, 15112, 312];
selection_sort(&mut a, |a, b| a <= b);
assert_eq!(is_sorted(&mut a, |a, b| a <= b), true);
selection_sort(&mut a, |a, b| a >= b);
assert_eq!(is_sorted(&mut a, |a, b| a >= b), true);
}
}

0 comments on commit af9cdda

Please sign in to comment.