Skip to content

Commit

Permalink
Merge pull request #9 from donjuanplatinum/donjuanplatinum
Browse files Browse the repository at this point in the history
Donjuanplatinum
  • Loading branch information
donjuanplatinum authored Jun 6, 2024
2 parents 2a34046 + 9e45672 commit 97cf643
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 39 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[package]
name = "algori"
version = "0.11.5"

version = "0.11.10"
edition = "2021"
authors = ["Donjuan Platinum <[email protected]>"]
license = "MIT"
description = "some common rust_algorithms, Everyone can participate, and the project will continue to be updated, all the algorithms comes from <Introduction to Algorithms III>"
license = "GPL-2.0-only"
description = "Rust Algorithms"
repository = "https://github.com/donjuanplatinum/Algori"
readme = "README.md"
documentation = "https://docs.rs/algori"
Expand Down
63 changes: 27 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
# Algorithms_Rust
[![crates.io](https://img.shields.io/crates/dr/algori)](https://crates.io/crates/algori)
[![Coverage Status](https://coveralls.io/repos/github/donjuanplatinum/Algori/badge.svg?branch=main)](https://coveralls.io/github/donjuanplatinum/Algori?branch=main)
[![Version](https://img.shields.io/crates/v/algori)](https://crates.io/crates/algori)

[![Chinese](./README.zh.md)](./README.zh.md)
[![English](./README.md)](./README.md)
<div align="center">

Algorithm_rust是一个rust的算法库
[![](https://img.shields.io/crates/d/algori.svg)](https://crates.io/crates/algori)
[![](https://img.shields.io/github/forks/barrensea/algori.svg)](https://github.com/BarrenSea/algori/fork)
[![](https://img.shields.io/github/repo-size/barrensea/algori.svg)](https://github.com/BarrenSea/algori)
[![](https://img.shields.io/github/stars/barrensea/algori.svg)](https://github.com/BarrenSea/algori)
[![](https://img.shields.io/github/commit-activity/t/barrensea/algori.svg)](https://github.com/BarrenSea/algori)

## 算法
### 排序
1. 二分插入排序
2. 泡泡排序
3. 计数排序
4. 堆排序
5. 插入排序
6. 分治排序
7. 快速排序
8. 基数排序
9. 选择排序
10. pdq排序
### 矩阵操作
1. 方阵相加
2. 方阵乘法
3. 矩阵结构
### 查找
1. 二分查找
2. 线性查找
</div>

### 数据结构
1. 最大优先队列
2.
3. 链表
### 子数组
1. 分治最大子数组
2. 线性最大子数组
### 数学算法与变换
1. 离散傅立叶变换
## 欢迎拉取请求
<p align="center">
<a href="https://github.com/barrensea/algori" rel="noopener">
<img width=200px height=200px src="./imgs/algori.png"></a>

<h3 align="center"><a href="https://join-lemmy.org">Algori</a></h3>
<p align="center">
Rust Algorithms
<br />
<br />
<a href="https://docs.rs/algori">Docs</a>
·
<a href="https://matrix.to/#/#algori:mozilla.org">Matrix</a>
</p>
</p>

## Algorithms
### Sorting
- [Insertionsort](./doc/sorting/README.md)
### Structures
- [Heap](./doc/structure/README.md)

10 changes: 10 additions & 0 deletions doc/sorting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Sorting 排序

+ 插入排序

## 插入排序
[![Insertion_sort](./insertion_sort.svg)](./insertion_sort.svg)

### 复杂度
最优时间复杂度: O(n)
最坏时间复杂度: O(n ^ 2)
1 change: 1 addition & 0 deletions doc/sorting/insertion_sort.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions doc/structure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Structure 数据结构

+ Heap/Priority Queue 堆/优先队列

## Heap/Priority Queue 堆/优先队列
[![Heap](./binary_heap_insert.svg)](./binary_heap_insert.svg)

1 change: 1 addition & 0 deletions doc/structure/binary_heap_insert.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/algori.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2024 Barrensea. Licensed under GPL-2

//! Algori - Rust Algorithms
//!
/// Sortion Algorithms
pub mod sorting;

/// Structures
pub mod structure;



62 changes: 62 additions & 0 deletions src/sorting/insertion_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/// # Insertion_Sort
pub fn insertion_sort<T>(array: &mut[T]) -> ()
where T: PartialOrd,
{
for point in 1..array.len() {
let mut current_point: usize = point;
while current_point > 0 && array[current_point] < array[current_point - 1] {
array.swap(current_point,current_point - 1);
current_point -= 1;
}
}
}

/// # Reverse Insertion_Sort
pub fn reverse_insertion_sort<T>(array: &mut[T]) -> ()
where T: PartialOrd,
{
for point in 1..array.len() {
let mut current_point: usize = point;
while current_point > 0 && array[current_point] > array[current_point - 1] {
array.swap(current_point,current_point - 1);
current_point -= 1;
}
}
}

#[cfg(test)]
mod insertion_sort_tests {
use super::{insertion_sort,reverse_insertion_sort};
use super::super::is_sorted;
#[test]
fn empty() -> ()
{
let mut a: [i32;0] = [];
insertion_sort(&mut a);

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

#[test]
fn one_element() -> ()
{
let mut a: [i32;1] = [1];
insertion_sort(&mut a);
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];
insertion_sort(&mut a);
assert_eq!(is_sorted(&mut a,|a,b| a <= b),true);
}
#[test]
fn reverse() -> ()
{
let mut a = [1,123,123,12,4234,42,1123,123,15112,312];
reverse_insertion_sort(&mut a);
assert_eq!(is_sorted(&mut a,|a,b| a >= b),true);
}
}

5 changes: 5 additions & 0 deletions src/sorting/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod utils;
mod insertion_sort;

pub use self::utils::*;
pub use self::insertion_sort::* ;
52 changes: 52 additions & 0 deletions src/sorting/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/// # Determine a slice if it is ordered
/// *Feature* : Use a Function to check if a slice is sorted or not
///
/// *Return* : if is sorted or not sorted, return true , if the element cant compare, return false
///
/// # Use
/// ```
/// use algori::sorting::{is_sorted,insertion_sort};
/// let mut a = [1,3,2,0,123,1,1,4634,341,2312452,351];
/// assert_eq!(is_sorted(&mut a, |a,b|a <=b),false);
///
/// insertion_sort(&mut a);
/// assert_eq!(is_sorted(&mut a,|a,b|a<=b),true);
/// ```
pub fn is_sorted<'a, T>(array: &'a [T],compare: fn(&T,&T)->bool) -> bool
where T: PartialOrd + 'a,
{
if array.len() == 0 {return true;}
for i in 0..array.len() - 1 {
match compare(&array[i],&array[i+1]) {
true => {continue;},
false => {return false;},
}
}
return true;
}


#[cfg(test)]
mod issorted_test {
use super::is_sorted;
#[test]
fn sorted() ->() {
let a = [1,2,3,4,5,6,111,321312321,623123124];
assert_eq!(is_sorted(& a, |a,b| a<=b),true);
}
#[test]
fn unsorted() -> () {
let a = [2,0,3,0,4,9,323,1,4,7,1,233,6,7];
assert_eq!(is_sorted(&a, |a,b| a <= b),false);
}
#[test]
fn char_compare() ->() {
let a: &[char] = &['a','b','c','d','e','f','g','h'];
assert_eq!(is_sorted(&a ,|a,b| a <= b),true);
}
#[test]
fn reverse() -> () {
let a = [7,6,5,4,3,2,1,0];
assert_eq!(is_sorted(&a,|a,b| a >= b),true);
}
}
Loading

0 comments on commit 97cf643

Please sign in to comment.