Skip to content

Instantly share code, notes, and snippets.

@ericho
Created February 20, 2017 14:57
Show Gist options
  • Save ericho/e84158ac2e6c39fb99df973830d1f199 to your computer and use it in GitHub Desktop.
Save ericho/e84158ac2e6c39fb99df973830d1f199 to your computer and use it in GitHub Desktop.
extern crate rand;
use rand::Rng;
use rand::distributions::{IndependentSample, Range};
use std::vec;
// Implement a bubble sort algorithm
// http://en.wikipedia.org/wiki/Bubble_sort
fn bubble_sort(vector: &mut Vec<i32>) {
let mut i = 0;
for i in vector {
println!("Value {}", i);
}
}
// Just create a vector of n elements
// with random values from 0 to 1000.
fn create_random_vector(n: i32) -> Vec<i32> {
let mut my_vector = vec::Vec::new();
let range = Range::new(0, 1000);
let mut random = rand::thread_rng();
for _ in 0..n {
my_vector.push(range.ind_sample(&mut random));
}
return my_vector
}
fn main() {
let max = 100;
// Create unordered vector
let mut vec = create_random_vector(max);
bubble_sort(&mut vec);
println!("Hello, world! {:?}", vec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment