Created
August 31, 2014 01:54
-
-
Save eu90h/e36d30cab26ce3496d21 to your computer and use it in GitHub Desktop.
an implementation of quicksort in rustlang
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 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