Skip to content

Instantly share code, notes, and snippets.

@kevinquinnyo
Created January 6, 2018 20:24
Show Gist options
  • Select an option

  • Save kevinquinnyo/d9beead2baa17a4da1e22c59c0f4dde6 to your computer and use it in GitHub Desktop.

Select an option

Save kevinquinnyo/d9beead2baa17a4da1e22c59c0f4dde6 to your computer and use it in GitHub Desktop.

Revisions

  1. kevinquinnyo created this gist Jan 6, 2018.
    25 changes: 25 additions & 0 deletions bubble_sort.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    fn main() {

    let mut array: [i32; 5] = [5, 6, 3, 2, 8];
    let sorted = bubble_sort(array);

    for x in &sorted {
    println!("{0}", x);
    }

    assert_eq!(sorted, [2, 3, 5, 6, 8], "Failed to sort array.");
    }

    fn bubble_sort(mut array: [i32; 5]) -> [i32; 5] {
    for i in 0..array.len() {
    for j in 0..array.len() - i - 1 {
    if array[j] > array[j + 1] {
    let tmp = array[j];
    array[j] = array[j + 1];
    array[j + 1] = tmp;
    }
    }
    }

    return array;
    }