Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ members = [
"crates/project_symbols",
"crates/prompt_store",
"crates/proto",
"crates/quicksort",
"crates/recent_projects",
"crates/refineable",
"crates/refineable/derive_refineable",
Expand Down Expand Up @@ -365,6 +366,7 @@ project_panel = { path = "crates/project_panel" }
project_symbols = { path = "crates/project_symbols" }
prompt_store = { path = "crates/prompt_store" }
proto = { path = "crates/proto" }
quicksort = { path = "crates/quicksort" }
recent_projects = { path = "crates/recent_projects" }
refineable = { path = "crates/refineable" }
release_channel = { path = "crates/release_channel" }
Expand Down
8 changes: 8 additions & 0 deletions crates/quicksort/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "quicksort"
version = "0.1.0"
edition = "2021"
publish = false

[lints]
workspace = true
99 changes: 99 additions & 0 deletions crates/quicksort/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
pub fn quicksort<T: Ord + Clone>(arr: &mut [T]) {
if arr.len() <= 1 {
return;
}
quicksort_helper(arr, 0, arr.len() - 1);
}

fn quicksort_helper<T: Ord + Clone>(arr: &mut [T], low: usize, high: usize) {
if low < high {
let pivot_idx = partition(arr, low, high);
if pivot_idx > 0 {
quicksort_helper(arr, low, pivot_idx - 1);
}
quicksort_helper(arr, pivot_idx + 1, high);
}
}

fn partition<T: Ord + Clone>(arr: &mut [T], low: usize, high: usize) -> usize {
let pivot = arr[high].clone();
let mut i = low;

for j in low..high {
if arr[j] <= pivot {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, high);
i
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_empty_array() {
let mut arr: Vec<i32> = vec![];
quicksort(&mut arr);
assert_eq!(arr, vec![]);
}

#[test]
fn test_single_element() {
let mut arr = vec![1];
quicksort(&mut arr);
assert_eq!(arr, vec![1]);
}

#[test]
fn test_sorted_array() {
let mut arr = vec![1, 2, 3, 4, 5];
quicksort(&mut arr);
assert_eq!(arr, vec![1, 2, 3, 4, 5]);
}

#[test]
fn test_reverse_sorted_array() {
let mut arr = vec![5, 4, 3, 2, 1];
quicksort(&mut arr);
assert_eq!(arr, vec![1, 2, 3, 4, 5]);
}

#[test]
fn test_unsorted_array() {
let mut arr = vec![3, 7, 8, 5, 2, 1, 9, 5, 4];
quicksort(&mut arr);
assert_eq!(arr, vec![1, 2, 3, 4, 5, 5, 7, 8, 9]);
}

#[test]
fn test_duplicates() {
let mut arr = vec![3, 3, 3, 1, 2, 1];
quicksort(&mut arr);
assert_eq!(arr, vec![1, 1, 2, 3, 3, 3]);
}

#[test]
fn test_negative_numbers() {
let mut arr = vec![-5, 3, -1, 0, 8, -3];
quicksort(&mut arr);
assert_eq!(arr, vec![-5, -3, -1, 0, 3, 8]);
}

#[test]
fn test_strings() {
let mut arr = vec!["banana", "apple", "cherry", "date"];
quicksort(&mut arr);
assert_eq!(arr, vec!["apple", "banana", "cherry", "date"]);
}

#[test]
fn test_large_array() {
let mut arr: Vec<i32> = (0..1000).rev().collect();
quicksort(&mut arr);
let expected: Vec<i32> = (0..1000).collect();
assert_eq!(arr, expected);
}
}