added bubble_sort, binary_search, linear_search

This commit is contained in:
specCon18 2024-05-16 22:19:50 -04:00
commit f895efce44
13 changed files with 305 additions and 0 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -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"

8
Cargo.toml Normal file
View file

@ -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]

26
flake.lock generated Normal file
View file

@ -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
}

31
flake.nix Normal file
View file

@ -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 = {
};
};
}

11
nix/default.nix Normal file
View file

@ -0,0 +1,11 @@
{ pkgs ? import <nixpkgs> { }, 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;
}

15
nix/devshell.nix Normal file
View file

@ -0,0 +1,15 @@
{ pkgs ? import <nixpkgs> { } }:
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
];
}

32
nix/flake.nix Normal file
View file

@ -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 = {
};
};
}

63
src/binary_search.rs Normal file
View file

@ -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<bool> {
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)
}

51
src/bubble_sort.rs Normal file
View file

@ -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;
}
}
}
}

54
src/linear_search.rs Normal file
View file

@ -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
}

5
src/main.rs Normal file
View file

@ -0,0 +1,5 @@
mod linear_search;
mod binary_search;
mod bubble_sort;
fn main() {
}