Last active
October 29, 2021 09:13
-
-
Save kampersanda/bb502bc90545f863e0d53cb47917b32b to your computer and use it in GitHub Desktop.
simple-sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut v = make_randoms(10000); | |
simple_sort(&mut v); | |
if varify(&v) { | |
println!("Sorted"); | |
} else { | |
println!("Unsorted"); | |
} | |
} | |
fn make_randoms(n: usize) -> Vec<u64> { | |
let mut x = 13; | |
let mut v = vec![0; n]; | |
for i in 0..v.len() { | |
x = splitmix64(x); | |
v[i] = x; | |
} | |
v | |
} | |
fn splitmix64(mut x: u64) -> u64 { | |
x = x.wrapping_add(0x9e3779b97f4a7c15); | |
x = (x ^ (x >> 30)).wrapping_mul(0xbf58476d1ce4e5b9); | |
x = (x ^ (x >> 27)).wrapping_mul(0x94d049bb133111eb); | |
x ^ (x >> 31) | |
} | |
fn simple_sort(v: &mut [u64]) { | |
for i in 0..v.len() { | |
for j in 0..v.len() { | |
if v[i] < v[j] { | |
let t = v[i]; | |
v[i] = v[j]; | |
v[j] = t; | |
} | |
} | |
} | |
} | |
fn varify(v: &[u64]) -> bool { | |
for i in 1..v.len() { | |
if v[i - 1] > v[i] { | |
return false; | |
} | |
} | |
true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment