Ai
2 Star 1 Fork 0

endpoint_rust/Rust

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
exchange_sort.rs 958 Bytes
一键复制 编辑 原始数据 按行查看 历史
merelymyself 提交于 2022-04-27 04:52 +08:00 . Add Exchange Sort (#318)
// sorts through swapping the first value until it is at the right position, and repeating for all the following.
pub fn exchange_sort(arr: &mut [i32]) {
let length = arr.len();
for number1 in 0..length {
for number2 in (number1 + 1)..length {
if arr[number2] < arr[number1] {
arr.swap(number1, number2)
}
}
}
}
#[cfg(test)]
mod tests {
use super::super::is_sorted;
use super::*;
#[test]
fn it_works() {
let mut arr1 = [6, 5, 4, 3, 2, 1];
exchange_sort(&mut arr1);
assert!(is_sorted(&arr1));
arr1 = [12, 343, 21, 90, 3, 21];
exchange_sort(&mut arr1);
assert!(is_sorted(&arr1));
let mut arr2 = [1];
exchange_sort(&mut arr2);
assert!(is_sorted(&arr2));
let mut arr3 = [213, 542, 90, -23412, -32, 324, -34, 3324, 54];
exchange_sort(&mut arr3);
assert!(is_sorted(&arr3));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Rust
1
https://gitee.com/endpoint_rust/Rust.git
git@gitee.com:endpoint_rust/Rust.git
endpoint_rust
Rust
Rust
master

搜索帮助