first sorting fkt

This commit is contained in:
hendrik 2024-06-29 07:03:24 +02:00
commit b83311a93f
5 changed files with 52 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "sorting"
version = "0.1.0"

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "sorting"
version = "0.1.0"
edition = "2021"
[dependencies]

22
src/main.rs Normal file
View File

@ -0,0 +1,22 @@
pub fn selection_sort(input: &mut Vec<i32>) {
let list_len = input.len();
for i in 0..list_len - 1 {
let mut min = i;
for j in i + 1..list_len {
if input[j] < input[min] {
min = j;
}
}
if i != min {
input.swap(i, min);
}
}
}
fn main() {
let mut foo = vec![4, 2, 6, 121, 6, 31, 4, 3];
selection_sort(&mut foo);
println!("{:?}", foo);
}

16
src/selection_sort.rs Normal file
View File

@ -0,0 +1,16 @@
pub fn selection_sort(input: &mut Vec<i32>) {
let list_len = input.len();
for i in 0..list_len - 1 {
let mut min = i;
for j in i + 1..list_len {
if input[j] < input[min] {
min = j;
}
}
if i != min {
input.swap(i, min);
}
}
}