Skip to content

Instantly share code, notes, and snippets.

@eu90h
Created August 31, 2014 01:54
Show Gist options
  • Save eu90h/e36d30cab26ce3496d21 to your computer and use it in GitHub Desktop.
Save eu90h/e36d30cab26ce3496d21 to your computer and use it in GitHub Desktop.
an implementation of quicksort in rustlang
fn choose_pivot(left: uint, right: uint) -> uint {
if left < right+1 {
std::rand::task_rng().gen_range(left, right+1)
} else {
left
}
}
fn partition(data: &mut[int], left: uint, right: uint) -> uint {
let pivotIndex: uint = choose_pivot(left, right);
let pivotValue: int = data[pivotIndex];
data.swap(pivotIndex, right);
let mut storeIndex: uint = left;
for i in range(left, right) {
if data[i] < pivotValue {
data.swap(storeIndex, i);
storeIndex += 1;
}
}
data.swap(storeIndex, right);
storeIndex
}
fn sort(data: &mut[int], left: uint, right: uint) {
if left < right && right < data.len() {
let p: uint = partition(data, left, right);
sort(data, left, p - 1);
sort(data, p + 1, right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment