Skip to content

Instantly share code, notes, and snippets.

@kinoshita-lab
Created August 12, 2020 03:02
Show Gist options
  • Save kinoshita-lab/96bac387fd4fd74093982364d4e2b273 to your computer and use it in GitHub Desktop.
Save kinoshita-lab/96bac387fd4fd74093982364d4e2b273 to your computer and use it in GitHub Desktop.
learning rust: bubblesort in main
[package]
name = "bubblesort"
version = "0.1.0"
authors = ["kinoshita-lab"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand ="*"
use rand::prelude::*;
fn main() {
let mut rng = rand::thread_rng();
let mut nums: Vec<i32> = (0..100).collect();
nums.shuffle(&mut rng);
println!("unsorted: {:?}", nums);
for i in 0..nums.len() - 1 {
for j in 1..nums.len() - i {
if nums[j] < nums[j - 1] {
nums.swap(j, j - 1)
}
}
}
println!("sorted: {:?}", nums);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment