Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(binary_search): 添加二分搜索 #21

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
### Sorting 排序
- [InsertionSort 插入排序](./doc/sorting/_index.md)
- [SelectionSort 选择排序](./doc/sorting/_index.md)
- [BubbleSort 冒泡排序](./doc/sorting/_index.md)
### Structures 数据结构
- [Heap 堆/优先队列](./doc/structure/_index.md)

### Searching 搜索
- [BinarySearch 二分搜索](./doc/searching/_index.md)

15 changes: 15 additions & 0 deletions doc/searching/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Searching 搜索
math: true
---

+ 二分搜索





## 二分搜索
`二分查找`是一种高效搜索算法 接收一个`有序序列`,每轮缩小`一半`搜索范围 直至找到目标元素或搜索区间为空为止

[![binarysearch](./binarysearch.gif)](./binarysearch.gif)
Binary file added doc/searching/binarysearch.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions doc/sorting/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ math: true
+ 选择排序
+ 冒泡排序

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



Expand Down
1 change: 1 addition & 0 deletions doc/structure/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ math: true
+ Heap/Priority Queue 堆/优先队列

## Heap/Priority Queue 堆/优先队列

[![Heap](./binary_heap_insert.svg)](./binary_heap_insert.svg)

3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ pub mod sorting;

/// Structures
pub mod structure;

/// Searching
pub mod searching;
51 changes: 51 additions & 0 deletions src/searching/binary_search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/// # Binary_Search
/// binary_search function receives a `positive`(PartialOrd) array and an element
/// - If this array contains this element, return `Ok(the index of the element)`
/// - If this array not contains this element, return `Err(the index of the insertion point)`
/// - `Insert point` is the index after the largest element smaller than the given element
/// # Example
/// ```
/// use algori::searching::binary_search;
/// let a = [0,1,3,4,9,20];
/// assert_eq!(Err(5),binary_search(&a,&10));
/// ```
pub fn binary_search<T: PartialOrd>(array: &[T], element: &T) -> Result<usize, usize> {
let mut left_idx: usize = 0;
let mut right_idx: usize = array.len();
let mut mid_idx: usize = 0;
while left_idx < right_idx {
mid_idx = (left_idx + right_idx) / 2;
if &array[mid_idx] < element {
left_idx = mid_idx + 1;
} else if &array[mid_idx] > element {
right_idx = mid_idx;
} else {
return Ok(mid_idx);
}
}
return Err(left_idx);
}

#[cfg(test)]
mod test {
use super::*;
macro_rules! test_cases {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (arr, element, expected) = $test_case;
assert_eq!(binary_search(arr, element), expected);
}
)*
};
}
test_cases! {
empty: (&[],&0,Err(0)),
one_element: (&[1],&1,Ok(0)),
one_element_not_found: (&[1],&0,Err(0)),
string_one: (&["a","b"],&"a",Ok(0)),
string: (&["a","b","c","e","g"],&"c",Ok(2)),
string_now_found: (&["a","b","c","e","g"],&"d",Err(3)),
}
}
2 changes: 2 additions & 0 deletions src/searching/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod binary_search;
pub use binary_search::*;
Loading