added demo in main for complete algos. started dijkstras
This commit is contained in:
parent
f895efce44
commit
fc92909aec
10 changed files with 262 additions and 15 deletions
1
.direnv/flake-profile
Symbolic link
1
.direnv/flake-profile
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
flake-profile-2-link
|
||||||
1
.direnv/flake-profile-1-link
Symbolic link
1
.direnv/flake-profile-1-link
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
/nix/store/vl0arj38f6w5dnj7vp00n0vdz9ywfv43-nix-shell-env
|
||||||
1
.direnv/flake-profile-2-link
Symbolic link
1
.direnv/flake-profile-2-link
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
/nix/store/i3rlyi8v4kyhgadm396y1cqv2grr7w14-nix-shell-env
|
||||||
|
|
@ -9,6 +9,7 @@ pkgs.mkShell {
|
||||||
rustc
|
rustc
|
||||||
rustup
|
rustup
|
||||||
clippy
|
clippy
|
||||||
|
bacon
|
||||||
rust-analyzer
|
rust-analyzer
|
||||||
pkg-config
|
pkg-config
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -1,49 +1,62 @@
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::binary_search;
|
use super::binary_search;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn binary_search_primegen_test() {
|
||||||
|
let haystack = [1, 3, 4, 69, 71, 81, 90, 99, 420, 1337, 69420];
|
||||||
|
assert_eq!(binary_search(&haystack, 69), Some((true, 3)));
|
||||||
|
assert_eq!(binary_search(&haystack, 1336), Some((false, 0)));
|
||||||
|
assert_eq!(binary_search(&haystack, 69420), Some((true, 10)));
|
||||||
|
assert_eq!(binary_search(&haystack, 69421), Some((false, 0)));
|
||||||
|
assert_eq!(binary_search(&haystack, 1), Some((true, 0)));
|
||||||
|
assert_eq!(binary_search(&haystack, 0), Some((false, 0)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_binary_search_found() {
|
fn test_binary_search_found() {
|
||||||
let haystack = [1, 3, 5, 7, 9];
|
let haystack = [1, 3, 5, 7, 9];
|
||||||
assert_eq!(binary_search(&haystack, 5), Some(true));
|
assert_eq!(binary_search(&haystack, 5), Some((true, 2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_binary_search_not_found() {
|
fn test_binary_search_not_found() {
|
||||||
let haystack = [1, 3, 5, 7, 9];
|
let haystack = [1, 3, 5, 7, 9];
|
||||||
assert_eq!(binary_search(&haystack, 4), Some(false));
|
assert_eq!(binary_search(&haystack, 4), Some((false, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_binary_search_empty() {
|
fn test_binary_search_empty() {
|
||||||
let haystack: [usize; 0] = [];
|
let haystack: [usize; 0] = [];
|
||||||
assert_eq!(binary_search(&haystack, 4), Some(false));
|
assert_eq!(binary_search(&haystack, 4), Some((false, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_binary_search_single_element_found() {
|
fn test_binary_search_single_element_found() {
|
||||||
let haystack = [5];
|
let haystack = [5];
|
||||||
assert_eq!(binary_search(&haystack, 5), Some(true));
|
assert_eq!(binary_search(&haystack, 5), Some((true, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_binary_search_single_element_not_found() {
|
fn test_binary_search_single_element_not_found() {
|
||||||
let haystack = [5];
|
let haystack = [5];
|
||||||
assert_eq!(binary_search(&haystack, 3), Some(false));
|
assert_eq!(binary_search(&haystack, 3), Some((false, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_binary_search_multiple_elements_not_found() {
|
fn test_binary_search_multiple_elements_not_found() {
|
||||||
let haystack = [1, 2, 3, 4, 5];
|
let haystack = [1, 2, 3, 4, 5];
|
||||||
assert_eq!(binary_search(&haystack, 6), Some(false));
|
assert_eq!(binary_search(&haystack, 6), Some((false, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_binary_search_multiple_elements_found() {
|
fn test_binary_search_multiple_elements_found() {
|
||||||
let haystack = [1, 2, 3, 4, 5];
|
let haystack = [1, 2, 3, 4, 5];
|
||||||
assert_eq!(binary_search(&haystack, 3), Some(true));
|
assert_eq!(binary_search(&haystack, 3), Some((true, 2)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn binary_search(haystack: &[usize], needle: usize) -> Option<bool> {
|
|
||||||
|
pub fn binary_search(haystack: &[usize], needle: usize) -> Option<(bool, usize)> {
|
||||||
let mut low = 0;
|
let mut low = 0;
|
||||||
let mut high = haystack.len();
|
let mut high = haystack.len();
|
||||||
|
|
||||||
|
|
@ -51,7 +64,7 @@ fn binary_search(haystack: &[usize], needle: usize) -> Option<bool> {
|
||||||
let midpoint = low + (high - low) / 2;
|
let midpoint = low + (high - low) / 2;
|
||||||
let value = haystack[midpoint];
|
let value = haystack[midpoint];
|
||||||
if value == needle {
|
if value == needle {
|
||||||
return Some(true);
|
return Some((true, midpoint));
|
||||||
} else if value > needle {
|
} else if value > needle {
|
||||||
high = midpoint;
|
high = midpoint;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -59,5 +72,5 @@ fn binary_search(haystack: &[usize], needle: usize) -> Option<bool> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(false)
|
Some((false, 0)) // Return 0 as index if value is not found
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,12 @@
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::bubble_sort;
|
use super::bubble_sort;
|
||||||
#[test]
|
#[test]
|
||||||
|
fn bubble_sort_primeagen_class_test(){
|
||||||
|
let mut arr = [9, 3, 7, 4, 69, 420, 42];
|
||||||
|
bubble_sort(&mut arr);
|
||||||
|
assert_eq!(arr,[3, 4, 7, 9, 42, 69, 420]);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
fn bubble_sort_already_sorted_input(){
|
fn bubble_sort_already_sorted_input(){
|
||||||
let mut arr = [1, 2, 3, 4, 5];
|
let mut arr = [1, 2, 3, 4, 5];
|
||||||
bubble_sort(&mut arr);
|
bubble_sort(&mut arr);
|
||||||
|
|
@ -37,7 +43,7 @@ mod tests {
|
||||||
assert_eq!(arr, [42]);
|
assert_eq!(arr, [42]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn bubble_sort(arr: &mut [usize]) {
|
pub fn bubble_sort(arr: &mut [usize]) {
|
||||||
let n = arr.len();
|
let n = arr.len();
|
||||||
for i in 0..n {
|
for i in 0..n {
|
||||||
for j in 0..n - 1 - i {
|
for j in 0..n - 1 - i {
|
||||||
|
|
|
||||||
102
src/dijkstras.rs
Normal file
102
src/dijkstras.rs
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::dijkstras_shortest_path;
|
||||||
|
#[test]
|
||||||
|
fn dijkstras_shortest_path_primeagen_class_test(){
|
||||||
|
// (B) --- (E) ---- (F)
|
||||||
|
// / | | /|
|
||||||
|
// (A) | ------|------- |
|
||||||
|
// \ |/ | |
|
||||||
|
// (C) --- (D) ---- (G)
|
||||||
|
let mut graph:WeightedAdjacencyList<&str,usize> = WeightedAdjacencyList::new();
|
||||||
|
graph.add_vertex("A");
|
||||||
|
graph.add_vertex("B");
|
||||||
|
graph.add_vertex("C");
|
||||||
|
graph.add_vertex("D");
|
||||||
|
graph.add_vertex("E");
|
||||||
|
graph.add_vertex("F");
|
||||||
|
graph.add_edge("A","B",3);
|
||||||
|
graph.add_edge("A","C",1);
|
||||||
|
graph.add_edge("B","A",3);
|
||||||
|
graph.add_edge("B","C",4);
|
||||||
|
graph.add_edge("B","E",1);
|
||||||
|
graph.add_edge("C","B",4);
|
||||||
|
graph.add_edge("C","D",7);
|
||||||
|
graph.add_edge("C","A",1);
|
||||||
|
graph.add_edge("D","C",7);
|
||||||
|
graph.add_edge("D","E",5);
|
||||||
|
graph.add_edge("D","G",1);
|
||||||
|
graph.add_edge("E","B",1);
|
||||||
|
graph.add_edge("E","D",5);
|
||||||
|
graph.add_edge("E","F",2);
|
||||||
|
graph.add_edge("F","G",1);
|
||||||
|
graph.add_edge("F","E",2);
|
||||||
|
graph.add_edge("F","C",18);
|
||||||
|
graph.add_edge("G","D",1);
|
||||||
|
graph.add_edge("G","F",1);
|
||||||
|
dijkstras_shortest_path(source,sink,graph);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Define a struct to represent the adjacency list with weighted edges
|
||||||
|
pub struct WeightedAdjacencyList<T, W> {
|
||||||
|
vertices: HashMap<T, HashMap<T, W>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, W> WeightedAdjacencyList<T, W>
|
||||||
|
where
|
||||||
|
T: std::hash::Hash + Eq + Clone,
|
||||||
|
{
|
||||||
|
// Constructor to create a new weighted adjacency list
|
||||||
|
fn new() -> Self {
|
||||||
|
WeightedAdjacencyList {
|
||||||
|
vertices: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to add a vertex to the adjacency list
|
||||||
|
fn add_vertex(&mut self, vertex: T) {
|
||||||
|
self.vertices.entry(vertex).or_insert(HashMap::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to add an edge with weight between two vertices
|
||||||
|
fn add_edge(&mut self, from: T, to: T, weight: W) {
|
||||||
|
// Ensure both vertices exist in the adjacency list
|
||||||
|
self.add_vertex(from.clone());
|
||||||
|
self.add_vertex(to.clone());
|
||||||
|
|
||||||
|
// Add the edge from 'from' to 'to' with weight
|
||||||
|
self.vertices.get_mut(&from).unwrap().insert(to, weight);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to get the weight of an edge between two vertices
|
||||||
|
fn get_weight(&self, from: &T, to: &T) -> Option<&W> {
|
||||||
|
self.vertices.get(from)?.get(to)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to get the neighbors of a vertex
|
||||||
|
fn get_neighbors(&self, vertex: &T) -> Option<&HashMap<T, W>> {
|
||||||
|
self.vertices.get(vertex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn has_unvisited(seen: &[bool], dists: &[f64]) -> bool {
|
||||||
|
return seen.iter().enumerate().any(|(i, &s)| !s && dists[i] < f64::INFINITY);
|
||||||
|
}
|
||||||
|
fn get_lowest_unvisited(seen: &[bool], dists: &[f64]) -> usize {
|
||||||
|
let mut idx = usize::MAX;
|
||||||
|
let mut lowest_distance = f64::INFINITY;
|
||||||
|
for (i, &is_seen) in seen.iter().enumerate() {
|
||||||
|
if is_seen {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if lowest_distance > dists[i] {
|
||||||
|
lowest_distance = dists[i];
|
||||||
|
idx = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
idx
|
||||||
|
}
|
||||||
|
pub fn dijkstras_shortest_path(source:usize,sink:usize,mut graph:WeightedAdjacencyList<&str,usize>){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,16 @@
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::linear_search;
|
use super::linear_search;
|
||||||
|
#[test]
|
||||||
|
fn linear_search_primeagen_test(){
|
||||||
|
let haystack = [1, 3, 4, 69, 71, 81, 90, 99, 420, 1337, 69420];
|
||||||
|
assert_eq!(linear_search(&haystack, 69), Some((true,3)));
|
||||||
|
assert_eq!(linear_search(&haystack, 1336), None);
|
||||||
|
assert_eq!(linear_search(&haystack, 69420), Some((true,10)));
|
||||||
|
assert_eq!(linear_search(&haystack, 69421), None);
|
||||||
|
assert_eq!(linear_search(&haystack, 1), Some((true,0)));
|
||||||
|
assert_eq!(linear_search(&haystack, 0), None);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linear_search_found() {
|
fn test_linear_search_found() {
|
||||||
let haystack = [1, 3, 5, 7, 9];
|
let haystack = [1, 3, 5, 7, 9];
|
||||||
|
|
@ -44,7 +53,7 @@ mod tests {
|
||||||
assert_eq!(linear_search(&haystack, 3), Some((true, 2)));
|
assert_eq!(linear_search(&haystack, 3), Some((true, 2)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn linear_search(haystack: &[usize], needle: usize) -> Option<(bool, usize)> {
|
pub fn linear_search(haystack: &[usize], needle: usize) -> Option<(bool, usize)> {
|
||||||
for (idx, &n) in haystack.iter().enumerate() {
|
for (idx, &n) in haystack.iter().enumerate() {
|
||||||
if n == needle {
|
if n == needle {
|
||||||
return Some((true, idx));
|
return Some((true, idx));
|
||||||
|
|
|
||||||
74
src/main.rs
74
src/main.rs
|
|
@ -1,5 +1,77 @@
|
||||||
mod linear_search;
|
mod linear_search;
|
||||||
mod binary_search;
|
mod binary_search;
|
||||||
mod bubble_sort;
|
mod bubble_sort;
|
||||||
fn main() {
|
mod quick_sort;
|
||||||
|
mod dijkstras;
|
||||||
|
fn linear_search_demo(){
|
||||||
|
println!("-------------------");
|
||||||
|
println!("LINEAR SEARCH DEMO:");
|
||||||
|
println!("-------------------");
|
||||||
|
let haystack = [1, 3, 4, 69, 71, 81, 90, 99, 420, 1337, 69420];
|
||||||
|
println!("Array being searched: {:?}",haystack);
|
||||||
|
let needle = 81;
|
||||||
|
let search = linear_search::linear_search(&haystack, needle);
|
||||||
|
match search {
|
||||||
|
Some((value, index)) => {
|
||||||
|
if value {
|
||||||
|
println!("Value {} found at index {}", needle, index);
|
||||||
|
} else {
|
||||||
|
println!("Value {} not found", needle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => println!("Error: Unable to perform linear search."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn binary_search_demo(){
|
||||||
|
println!("-------------------");
|
||||||
|
println!("BINARY SEARCH DEMO:");
|
||||||
|
println!("-------------------");
|
||||||
|
let haystack = [1, 3, 4, 69, 71, 81, 90, 99, 420, 1337, 69420];
|
||||||
|
println!("Array being searched: {:?}",haystack);
|
||||||
|
let needle = 69;
|
||||||
|
let search = binary_search::binary_search(&haystack,needle);
|
||||||
|
match search {
|
||||||
|
Some((value, index)) => {
|
||||||
|
if value {
|
||||||
|
println!("Value {} found at index {}", needle, index);
|
||||||
|
} else {
|
||||||
|
println!("Value {} not found", needle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => println!("Error: Unable to perform linear search."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn bubble_sort_demo(){
|
||||||
|
println!("-------------------");
|
||||||
|
println!("BUBBLE SORT DEMO:");
|
||||||
|
println!("-------------------");
|
||||||
|
let mut arr = [9, 3, 7, 4, 69, 420, 42];
|
||||||
|
println!("Array before quick sort: {:?}",arr);
|
||||||
|
bubble_sort::bubble_sort(&mut arr);
|
||||||
|
println!("Array after quick sort: {:?}",arr);
|
||||||
|
}
|
||||||
|
fn quick_sort_demo(){
|
||||||
|
println!("-------------------");
|
||||||
|
println!("QUICK SORT DEMO:");
|
||||||
|
println!("-------------------");
|
||||||
|
let mut arr = [9, 3, 7, 4, 69, 420, 42];
|
||||||
|
println!("Array before quick sort: {:?}",arr);
|
||||||
|
quick_sort::quick_sort(&mut arr);
|
||||||
|
println!("Array after quick sort: {:?}",arr);
|
||||||
|
}
|
||||||
|
fn main() {
|
||||||
|
println!("");
|
||||||
|
println!("|~~~~~~~~~~~~~~~|");
|
||||||
|
println!("SEARCHING DEMOS:");
|
||||||
|
println!("|~~~~~~~~~~~~~~~|");
|
||||||
|
println!("");
|
||||||
|
linear_search_demo();
|
||||||
|
binary_search_demo();
|
||||||
|
println!("");
|
||||||
|
println!("|~~~~~~~~~~~~~~~|");
|
||||||
|
println!("SORTING DEMOS:");
|
||||||
|
println!("|~~~~~~~~~~~~~~~|");
|
||||||
|
println!("");
|
||||||
|
quick_sort_demo();
|
||||||
|
bubble_sort_demo();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
41
src/quick_sort.rs
Normal file
41
src/quick_sort.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::quick_sort;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn quick_sort_primeagen_class_test() {
|
||||||
|
let mut arr = vec![9, 3, 7, 4, 69, 420, 42];
|
||||||
|
quick_sort(&mut arr);
|
||||||
|
assert_eq!(arr, vec![3, 4, 7, 9, 42, 69, 420]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn partition(arr: &mut [usize], low: usize, high: usize) -> usize {
|
||||||
|
let pivot = arr[high];
|
||||||
|
let mut idx = low;
|
||||||
|
for i in low..high {
|
||||||
|
if arr[i] <= pivot {
|
||||||
|
arr.swap(i, idx);
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arr.swap(idx, high);
|
||||||
|
idx
|
||||||
|
}
|
||||||
|
|
||||||
|
fn qs(arr: &mut [usize], low: usize, high: usize) {
|
||||||
|
if low < high {
|
||||||
|
let pivot_idx = partition(arr, low, high);
|
||||||
|
if pivot_idx > 0 {
|
||||||
|
qs(arr, low, pivot_idx - 1);
|
||||||
|
}
|
||||||
|
qs(arr, pivot_idx + 1, high);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn quick_sort(arr: &mut [usize]) {
|
||||||
|
let len = arr.len();
|
||||||
|
if len > 0 {
|
||||||
|
qs(arr, 0, len - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue