diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a1ad4161d..926e4fbe8 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -5,3 +5,4 @@ Gathros Jeremie Gillet (- Jie -) Salim Khatib Hitesh C +Jess 3Jane diff --git a/chapters/sorting_searching/bogo/bogo_sort.md b/chapters/sorting_searching/bogo/bogo_sort.md index 3a643ecef..36f7da1f7 100644 --- a/chapters/sorting_searching/bogo/bogo_sort.md +++ b/chapters/sorting_searching/bogo/bogo_sort.md @@ -27,6 +27,8 @@ In code, it looks something like this: [import, lang:"haskell"](code/haskell/bogoSort.hs) {% sample lang="cpp" %} [import, lang:"c_cpp"](code/c++/bogosort.cpp) +{% sample lang="rs" %} +[import, lang:"rust"](code/rust/bogosort.rs) {% endmethod %} That's it. diff --git a/chapters/sorting_searching/bogo/code/rust/bogosort.rs b/chapters/sorting_searching/bogo/code/rust/bogosort.rs new file mode 100644 index 000000000..e2a726998 --- /dev/null +++ b/chapters/sorting_searching/bogo/code/rust/bogosort.rs @@ -0,0 +1,28 @@ +// Submitted by jess 3jane + +extern crate rand; + +use rand::{thread_rng, Rng}; + +fn is_sorted(arr : &[i32]) -> bool { + for i in 1..arr.len() { + if arr[i-1] > arr[i] { + return false; + } + } + true +} + +fn bogo_sort(arr : &mut [i32]) { + while !is_sorted(arr) { + thread_rng().shuffle(arr); + } +} + +fn main() { + let mut v = vec![1, 2, 3, 4, 5]; + thread_rng().shuffle(&mut v); + println!("Original array: {:?}", v); + bogo_sort(&mut v); + println!("Sorted array: {:?}", v); +}