Skip to content
Open
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
24 changes: 24 additions & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Check

on:
push:
branches:
- main
pull_request:
branches:
- 'main'

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- uses: actions-rs/cargo@v1
with:
command: build
- uses: actions-rs/cargo@v1
with:
command: test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "rust-algorithms"
version = "0.1.0"
edition = "2021"
19 changes: 19 additions & 0 deletions src/bubble_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub fn bubble_sort(mut vector: Vec<i32>) -> Vec<i32> {
let mut outer_index = 0;
let length = vector.len();

while outer_index < length {
let mut inner_index = outer_index + 1;
while inner_index < length {
if vector[inner_index] < vector[inner_index - 1] {
let tmp = vector[inner_index];
vector[inner_index] = vector[inner_index - 1];
vector[inner_index - 1] = tmp;
}
inner_index += 1;
}
outer_index += 1;
}

return vector;
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bubble_sort;
57 changes: 57 additions & 0 deletions tests/bubble_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// use rust_algorithms::bubble_sort;
use rust_algorithms::bubble_sort::bubble_sort;

fn is_sorted(vector: &Vec<i32>) -> bool {
let mut previous: Option<i32> = None;
for el in vector.iter() {
match previous {
Some(x) => {
if x > *el {
return false;
}
}
_ => {
previous = Some(*el);
}
}
}

true
}

#[test]
fn it_works() {
let vector = Vec::new();
bubble_sort(vector);
}

#[test]
fn it_works_for_one_element() {
let mut vector: Vec<i32> = vec![1];
vector = bubble_sort(vector);
assert!(is_sorted(&vector));
}

#[test]
fn it_works_for_two_elements_sorted() {
let mut vector: Vec<i32> = vec![1, 2];
vector = bubble_sort(vector);
assert!(is_sorted(&vector));
}

#[test]
fn it_works_for_two_elements_unsorted() {
let mut vector: Vec<i32> = vec![2, 1];
vector = bubble_sort(vector);
assert!(is_sorted(&vector));
}

#[test]
fn it_works_for_a_bigger_vector() {
let mut vector: Vec<i32> = vec![
213, 12, 123432, 11224, 6867, 32413, 78787653, 1213, 122, 4446, 146575, 1123245, 654654,
1231,
];
vector = bubble_sort(vector);
assert!(is_sorted(&vector));
}