commit f895efce449d9311d6a69afc28d74327c49821fe Author: specCon18 Date: Thu May 16 22:19:50 2024 -0400 added bubble_sort, binary_search, linear_search diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a36f161 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "algos-in-rs" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..933bd07 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "algos-in-rs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..259afea --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1715774670, + "narHash": "sha256-iJYnKMtLi5u6hZhJm94cRNSDG5Rz6ZzIkGbhPFtDRm0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b3fcfcfabd01b947a1e4f36622bbffa3985bdac6", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixpkgs-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..34b25d5 --- /dev/null +++ b/flake.nix @@ -0,0 +1,31 @@ +{ + description = "SunServer"; + + inputs={ + nixpkgs.url = "nixpkgs/nixpkgs-unstable"; + }; + outputs = { self, nixpkgs }@inputs: + let + supportedSystems = [ "x86_64-linux" ]; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + #v I improved how you call in packages + pkgs = forAllSystems (system: + import nixpkgs { + inherit system; + #v this can be adjusted to read args passed without impure later + # config = { allowUnfreePredicate = pkg: builtins.elem (nixpkgs.lib.getName pkg) [ + # ]; + # }; + } + ); + in { + packages = forAllSystems (system: { + default = pkgs.${system}.callPackage ./nix/default.nix { }; + }); + devShells = forAllSystems (system: { + default = pkgs.${system}.callPackage ./nix/devshell.nix { }; + }); + nixConfig = { + }; + }; +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..9f0d7de --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,11 @@ +{ pkgs ? import { }, lib }: + +pkgs.rustPlatform.buildRustPackage rec { + pname = "algos-in-rust"; + version = "1.0.0"; + cargoLock.lockFile = ../Cargo.lock; + src = pkgs.lib.cleanSource ../.; + buildInputs = [ ]; + nativeBuildInputs = [ pkgs.pkg-config ]; + doCheck = false; +} diff --git a/nix/devshell.nix b/nix/devshell.nix new file mode 100644 index 0000000..582ea50 --- /dev/null +++ b/nix/devshell.nix @@ -0,0 +1,15 @@ +{ pkgs ? import { } }: +pkgs.mkShell { + # Get dependencies from the main package + inputsFrom = [ (pkgs.callPackage ./default.nix { }) ]; + # Additional tooling + buildInputs = with pkgs; [ + cargo + cargo-watch + rustc + rustup + clippy + rust-analyzer + pkg-config + ]; +} diff --git a/nix/flake.nix b/nix/flake.nix new file mode 100644 index 0000000..405b72b --- /dev/null +++ b/nix/flake.nix @@ -0,0 +1,32 @@ +{ + description = "SunServer"; + + inputs={ + nixpkgs.url = "nixpkgs/nixpkgs-unstable"; + }; + outputs = { self, nixpkgs }@inputs: + let + supportedSystems = [ "x86_64-linux" ]; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + #v I improved how you call in packages + pkgs = forAllSystems (system: + import nixpkgs { + inherit system; + #v this can be adjusted to read args passed without impure later + config = { allowUnfreePredicate = pkg: builtins.elem (nixpkgs.lib.getName pkg) [ + "unrar" + ]; + }; + } + ); + in { + packages = forAllSystems (system: { + default = pkgs.${system}.callPackage ./nix/default.nix { }; + }); + devShells = forAllSystems (system: { + default = pkgs.${system}.callPackage ./nix/devshell.nix { }; + }); + nixConfig = { + }; + }; +} diff --git a/src/binary_search.rs b/src/binary_search.rs new file mode 100644 index 0000000..ee7ecbf --- /dev/null +++ b/src/binary_search.rs @@ -0,0 +1,63 @@ +#[cfg (test)] +mod tests { + use super::binary_search; + #[test] + fn test_binary_search_found() { + let haystack = [1, 3, 5, 7, 9]; + assert_eq!(binary_search(&haystack, 5), Some(true)); + } + + #[test] + fn test_binary_search_not_found() { + let haystack = [1, 3, 5, 7, 9]; + assert_eq!(binary_search(&haystack, 4), Some(false)); + } + + #[test] + fn test_binary_search_empty() { + let haystack: [usize; 0] = []; + assert_eq!(binary_search(&haystack, 4), Some(false)); + } + + #[test] + fn test_binary_search_single_element_found() { + let haystack = [5]; + assert_eq!(binary_search(&haystack, 5), Some(true)); + } + + #[test] + fn test_binary_search_single_element_not_found() { + let haystack = [5]; + assert_eq!(binary_search(&haystack, 3), Some(false)); + } + + #[test] + fn test_binary_search_multiple_elements_not_found() { + let haystack = [1, 2, 3, 4, 5]; + assert_eq!(binary_search(&haystack, 6), Some(false)); + } + + #[test] + fn test_binary_search_multiple_elements_found() { + let haystack = [1, 2, 3, 4, 5]; + assert_eq!(binary_search(&haystack, 3), Some(true)); + } +} +fn binary_search(haystack: &[usize], needle: usize) -> Option { + let mut low = 0; + let mut high = haystack.len(); + + while low < high { + let midpoint = low + (high - low) / 2; + let value = haystack[midpoint]; + if value == needle { + return Some(true); + } else if value > needle { + high = midpoint; + } else { + low = midpoint + 1; + } + } + + Some(false) +} diff --git a/src/bubble_sort.rs b/src/bubble_sort.rs new file mode 100644 index 0000000..e71c2c7 --- /dev/null +++ b/src/bubble_sort.rs @@ -0,0 +1,51 @@ + +#[cfg(test)] +mod tests { + use super::bubble_sort; + #[test] + fn bubble_sort_already_sorted_input(){ + let mut arr = [1, 2, 3, 4, 5]; + bubble_sort(&mut arr); + assert_eq!(arr, [1, 2, 3, 4, 5]); + } + + #[test] + fn bubble_sort_reverse_sorted_input(){ + let mut arr = [5, 4, 3, 2, 1]; + bubble_sort(&mut arr); + assert_eq!(arr, [1, 2, 3, 4, 5]); + } + + #[test] + fn bubble_sort_input_w_duplicate_elems(){ + let mut arr = [3, 1, 4, 1, 5, 9, 2, 6, 5]; + bubble_sort(&mut arr); + assert_eq!(arr, [1, 1, 2, 3, 4, 5, 5, 6, 9]); + } + + #[test] + fn bubble_sort_empty_input(){ + let mut arr: [usize; 0] = []; + bubble_sort(&mut arr); + assert_eq!(arr, []); + } + + #[test] + fn bubble_sort_input_w_one_elem(){ + let mut arr = [42]; + bubble_sort(&mut arr); + assert_eq!(arr, [42]); + } +} +fn bubble_sort(arr: &mut [usize]) { + let n = arr.len(); + for i in 0..n { + for j in 0..n - 1 - i { + if arr[j] > arr[j + 1] { + let tmp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = tmp; + } + } + } +} diff --git a/src/linear_search.rs b/src/linear_search.rs new file mode 100644 index 0000000..47ab058 --- /dev/null +++ b/src/linear_search.rs @@ -0,0 +1,54 @@ +#[cfg(test)] +mod tests { + use super::linear_search; + + #[test] + fn test_linear_search_found() { + let haystack = [1, 3, 5, 7, 9]; + assert_eq!(linear_search(&haystack, 5), Some((true, 2))); + } + + #[test] + fn test_linear_search_not_found() { + let haystack = [1, 3, 5, 7, 9]; + assert_eq!(linear_search(&haystack, 4), None); + } + + #[test] + fn test_linear_search_empty() { + let haystack: [usize; 0] = []; + assert_eq!(linear_search(&haystack, 4), None); + } + + #[test] + fn test_linear_search_single_element_found() { + let haystack = [5]; + assert_eq!(linear_search(&haystack, 5), Some((true, 0))); + } + + #[test] + fn test_linear_search_single_element_not_found() { + let haystack = [5]; + assert_eq!(linear_search(&haystack, 3), None); + } + + #[test] + fn test_linear_search_multiple_elements_not_found() { + let haystack = [1, 2, 3, 4, 5]; + assert_eq!(linear_search(&haystack, 6), None); + } + + #[test] + fn test_linear_search_multiple_elements_found() { + let haystack = [1, 2, 3, 4, 5]; + assert_eq!(linear_search(&haystack, 3), Some((true, 2))); + } +} +fn linear_search(haystack: &[usize], needle: usize) -> Option<(bool, usize)> { + for (idx, &n) in haystack.iter().enumerate() { + if n == needle { + return Some((true, idx)); + } + } + None +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2e0263d --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +mod linear_search; +mod binary_search; +mod bubble_sort; +fn main() { +}