-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add algorithms * adjust operation indexes
- Loading branch information
Showing
7 changed files
with
276 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use super::{ | ||
AlgorithmContext, | ||
Operation::{Compare, Noop, Swap}, | ||
}; | ||
|
||
pub const NAME: &str = "comb sort"; | ||
|
||
pub fn sort(nums: &mut [i32], ctx: &dyn AlgorithmContext) { | ||
let n = nums.len(); | ||
let mut gap = n; | ||
let shrink_factor = 1.3; | ||
let mut swapped = true; | ||
|
||
while gap > 1 || swapped { | ||
gap = (gap as f64 / shrink_factor).floor() as usize; | ||
if gap < 1 { | ||
gap = 1; | ||
} | ||
|
||
swapped = false; | ||
|
||
for i in 0..n - gap { | ||
let j = i + gap; | ||
ctx.next(Compare(i, j), nums.to_vec()); | ||
if nums[i] > nums[j] { | ||
nums.swap(i, j); | ||
ctx.next(Swap(i, j), nums.to_vec()); | ||
swapped = true; | ||
} | ||
} | ||
} | ||
|
||
ctx.next(Noop(), nums.to_vec()); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sorting::has_nums; | ||
use crate::sorting::is_sorted; | ||
use crate::sorting::NoopContext; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_sort() { | ||
let nums = &mut [3, 5, 2, 8, 6, 9, 0, 1, 4, 7]; | ||
sort(nums, &NoopContext); | ||
assert!(is_sorted(nums)); | ||
assert!(has_nums(nums)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use super::{ | ||
AlgorithmContext, | ||
Operation::{Compare, Noop, Swap}, | ||
}; | ||
|
||
pub const NAME: &str = "heap sort"; | ||
|
||
pub fn sort(nums: &mut [i32], ctx: &dyn AlgorithmContext) { | ||
let n = nums.len(); | ||
for i in (0..n / 2).rev() { | ||
heapify(nums, n, i, ctx); | ||
} | ||
for i in (1..n).rev() { | ||
nums.swap(0, i); | ||
ctx.next(Swap(0, i), nums.to_vec()); | ||
heapify(nums, i, 0, ctx); | ||
} | ||
ctx.next(Noop(), nums.to_vec()); | ||
} | ||
|
||
fn heapify(nums: &mut [i32], n: usize, i: usize, ctx: &dyn AlgorithmContext) { | ||
let mut largest = i; | ||
let left = 2 * i + 1; | ||
let right = 2 * i + 2; | ||
|
||
if left < n { | ||
ctx.next(Compare(left, largest), nums.to_vec()); | ||
if nums[left] > nums[largest] { | ||
largest = left; | ||
} | ||
} | ||
|
||
if right < n { | ||
ctx.next(Compare(right, largest), nums.to_vec()); | ||
if nums[right] > nums[largest] { | ||
largest = right; | ||
} | ||
} | ||
|
||
if largest != i { | ||
nums.swap(i, largest); | ||
ctx.next(Swap(i, largest), nums.to_vec()); | ||
heapify(nums, n, largest, ctx); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sorting::has_nums; | ||
use crate::sorting::is_sorted; | ||
use crate::sorting::NoopContext; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_sort() { | ||
let nums = &mut [3, 5, 2, 8, 6, 9, 0, 1, 4, 7]; | ||
sort(nums, &NoopContext); | ||
assert!(is_sorted(nums)); | ||
assert!(has_nums(nums)); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use super::{ | ||
AlgorithmContext, | ||
Operation::{Compare, Noop, Swap}, | ||
}; | ||
|
||
pub const NAME: &str = "quick sort"; | ||
|
||
pub fn sort(nums: &mut [i32], ctx: &dyn AlgorithmContext) { | ||
quick_sort_recursive(nums, 0, nums.len() - 1, ctx); | ||
ctx.next(Noop(), nums.to_vec()); | ||
} | ||
|
||
fn quick_sort_recursive(nums: &mut [i32], low: usize, high: usize, ctx: &dyn AlgorithmContext) { | ||
if low < high { | ||
let pivot_index = partition(nums, low, high, ctx); | ||
|
||
if pivot_index > 0 { | ||
quick_sort_recursive(nums, low, pivot_index - 1, ctx); | ||
} | ||
|
||
quick_sort_recursive(nums, pivot_index + 1, high, ctx); | ||
} | ||
} | ||
|
||
fn partition(nums: &mut [i32], low: usize, high: usize, ctx: &dyn AlgorithmContext) -> usize { | ||
let pivot = nums[high]; | ||
let mut i = low; | ||
|
||
for j in low..high { | ||
ctx.next(Compare(j, high), nums.to_vec()); | ||
if nums[j] <= pivot { | ||
if i != j { | ||
nums.swap(i, j); | ||
ctx.next(Swap(i, j), nums.to_vec()); | ||
} | ||
i += 1; | ||
} | ||
} | ||
|
||
if i != high { | ||
nums.swap(i, high); | ||
ctx.next(Swap(i, high), nums.to_vec()); | ||
} | ||
|
||
return i; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sorting::has_nums; | ||
use crate::sorting::is_sorted; | ||
use crate::sorting::NoopContext; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_sort() { | ||
let nums = &mut [3, 5, 2, 8, 6, 9, 0, 1, 4, 7]; | ||
sort(nums, &NoopContext); | ||
assert!(is_sorted(nums)); | ||
assert!(has_nums(nums)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use super::{ | ||
AlgorithmContext, | ||
Operation::{Compare, Noop, Swap}, | ||
}; | ||
|
||
pub const NAME: &str = "shell sort"; | ||
|
||
pub fn sort(nums: &mut [i32], ctx: &dyn AlgorithmContext) { | ||
let n = nums.len(); | ||
let mut gap = n / 2; | ||
while gap > 0 { | ||
for i in gap..n { | ||
let mut j = i; | ||
while j >= gap { | ||
ctx.next(Compare(j - gap, j), nums.to_vec()); | ||
if nums[j - gap] > nums[j] { | ||
nums.swap(j - gap, j); | ||
ctx.next(Swap(j - gap, j), nums.to_vec()); | ||
j -= gap; | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
gap /= 2; | ||
} | ||
ctx.next(Noop(), nums.to_vec()); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sorting::has_nums; | ||
use crate::sorting::is_sorted; | ||
use crate::sorting::NoopContext; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_sort() { | ||
let nums = &mut [3, 5, 2, 8, 6, 9, 0, 1, 4, 7]; | ||
sort(nums, &NoopContext); | ||
assert!(is_sorted(nums)); | ||
assert!(has_nums(nums)); | ||
} | ||
} |