Skip to content

Instantly share code, notes, and snippets.

@HarrisonHemstreet
Created June 3, 2023 00:28
Show Gist options
  • Save HarrisonHemstreet/c5ab37b27dca0dd20b653b8dc950d064 to your computer and use it in GitHub Desktop.
Save HarrisonHemstreet/c5ab37b27dca0dd20b653b8dc950d064 to your computer and use it in GitHub Desktop.
This is my homebrew quick sort. I don't think it's really quick sort, but it's close! :)
fn quick_sort_homebrew(mut vec1: Vec<i32>, pivot_index: usize) -> Vec<i32> {
println!("func ran");
let mut swap_position: usize = 0;
for num in &vec1 {
if num < &vec1[pivot_index] {
swap_position += 1;
}
}
println!("before swap: vec1: {:?}", vec1);
vec1.swap(pivot_index, swap_position);
println!("after swap: vec1: {:?}", vec1);
let mut i: usize = 1;
loop {
if i >= vec1.len() {
return vec1;
}
if vec1[i - 1] > vec1[i] {
break;
}
i += 1;
}
let mut new_pivot_index: usize = 0;
let mut j: usize = 0;
loop {
if j >= vec1.len() {
break;
}
if vec1[j] > vec1[j + 1] {
new_pivot_index = j;
break;
}
j += 1;
}
quick_sort_homebrew(vec1, new_pivot_index)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment