repo stringlengths 6 65 | file_url stringlengths 81 311 | file_path stringlengths 6 227 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 7
values | commit_sha stringlengths 40 40 | retrieved_at stringdate 2026-01-04 15:31:58 2026-01-04 20:25:31 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/selection_sort.rs | src/sorting/selection_sort.rs | pub fn selection_sort<T: Ord>(arr: &mut [T]) {
let len = arr.len();
for left in 0..len {
let mut smallest = left;
for right in (left + 1)..len {
if arr[right] < arr[smallest] {
smallest = right;
}
}
arr.swap(smallest, left);
}
}
#[cfg(... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/dutch_national_flag_sort.rs | src/sorting/dutch_national_flag_sort.rs | /*
A Rust implementation of the Dutch National Flag sorting algorithm.
Reference implementation: https://github.com/TheAlgorithms/Python/blob/master/sorts/dutch_national_flag_sort.py
More info: https://en.wikipedia.org/wiki/Dutch_national_flag_problem
*/
#[derive(PartialOrd, PartialEq, Eq)]
pub enum Colors {
Red,... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/counting_sort.rs | src/sorting/counting_sort.rs | /// In place counting sort for collections of u32
/// O(n + maxval) in time, where maxval is the biggest value an input can possibly take
/// O(maxval) in memory
/// u32 is chosen arbitrarly, a counting sort probably should'nt be used on data that requires bigger types.
pub fn counting_sort(arr: &mut [u32], maxval: us... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/radix_sort.rs | src/sorting/radix_sort.rs | /// Sorts the elements of `arr` in-place using radix sort.
///
/// Time complexity is `O((n + b) * logb(k))`, where `n` is the number of elements,
/// `b` is the base (the radix), and `k` is the largest element.
/// When `n` and `b` are roughly the same maginitude, this algorithm runs in linear time.
///
/// Space comp... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/wave_sort.rs | src/sorting/wave_sort.rs | /// Wave Sort Algorithm
///
/// Wave Sort is a sorting algorithm that works in O(n log n) time assuming
/// the sort function used works in O(n log n) time.
/// It arranges elements in an array into a sequence where every alternate
/// element is either greater or smaller than its adjacent elements.
///
/// Reference:
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/stooge_sort.rs | src/sorting/stooge_sort.rs | fn _stooge_sort<T: Ord>(arr: &mut [T], start: usize, end: usize) {
if arr[start] > arr[end] {
arr.swap(start, end);
}
if start + 1 >= end {
return;
}
let k = (end - start + 1) / 3;
_stooge_sort(arr, start, end - k);
_stooge_sort(arr, start + k, end);
_stooge_sort(arr, ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/bead_sort.rs | src/sorting/bead_sort.rs | //Bead sort only works for sequences of non-negative integers.
//https://en.wikipedia.org/wiki/Bead_sort
pub fn bead_sort(a: &mut [usize]) {
// Find the maximum element
let mut max = a[0];
(1..a.len()).for_each(|i| {
if a[i] > max {
max = a[i];
}
});
// allocating memory... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/bogo_sort.rs | src/sorting/bogo_sort.rs | use crate::math::PCG32;
use std::time::{SystemTime, UNIX_EPOCH};
const DEFAULT: u64 = 4294967296;
fn is_sorted<T: Ord>(arr: &[T], len: usize) -> bool {
for i in 0..len - 1 {
if arr[i] > arr[i + 1] {
return false;
}
}
true
}
#[cfg(target_pointer_width = "64")]
fn generate_inde... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/gnome_sort.rs | src/sorting/gnome_sort.rs | use std::cmp;
pub fn gnome_sort<T>(arr: &[T]) -> Vec<T>
where
T: cmp::PartialEq + cmp::PartialOrd + Clone,
{
let mut arr = arr.to_vec();
let mut i: usize = 1;
let mut j: usize = 2;
while i < arr.len() {
if arr[i - 1] < arr[i] {
i = j;
j = i + 1;
} else {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/sleep_sort.rs | src/sorting/sleep_sort.rs | use std::sync::mpsc;
use std::thread;
use std::time::Duration;
pub fn sleep_sort(vec: &[usize]) -> Vec<usize> {
let len = vec.len();
let (tx, rx) = mpsc::channel();
for &x in vec.iter() {
let tx: mpsc::Sender<usize> = tx.clone();
thread::spawn(move || {
thread::sleep(Duration::... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/tree_sort.rs | src/sorting/tree_sort.rs | // Author : cyrixninja
// Tree Sort Algorithm
// https://en.wikipedia.org/wiki/Tree_sort
// Wikipedia :A tree sort is a sort algorithm that builds a binary search tree from the elements to be sorted, and then traverses the tree (in-order) so that the elements come out in sorted order.
// Its typical use is sorting elem... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/bubble_sort.rs | src/sorting/bubble_sort.rs | pub fn bubble_sort<T: Ord>(arr: &mut [T]) {
if arr.is_empty() {
return;
}
let mut sorted = false;
let mut n = arr.len();
while !sorted {
sorted = true;
for i in 0..n - 1 {
if arr[i] > arr[i + 1] {
arr.swap(i, i + 1);
sorted = false;... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/tim_sort.rs | src/sorting/tim_sort.rs | //! Implements Tim sort algorithm.
//!
//! Tim sort is a hybrid sorting algorithm derived from merge sort and insertion sort.
//! It is designed to perform well on many kinds of real-world data.
use crate::sorting::insertion_sort;
use std::cmp;
static MIN_MERGE: usize = 32;
/// Calculates the minimum run length for ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/pigeonhole_sort.rs | src/sorting/pigeonhole_sort.rs | // From Wikipedia: Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements where the number of elements (n) and the length of the range of possible key values (N) are approximately the same. It requires O(n + N) time.
pub fn pigeonhole_sort(array: &mut [i32]) {
if let (Some(min), S... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/comb_sort.rs | src/sorting/comb_sort.rs | pub fn comb_sort<T: Ord>(arr: &mut [T]) {
let mut gap = arr.len();
let shrink = 1.3;
let mut sorted = false;
while !sorted {
gap = (gap as f32 / shrink).floor() as usize;
if gap <= 1 {
gap = 1;
sorted = true;
}
for i in 0..arr.len() - gap {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/mod.rs | src/sorting/mod.rs | mod bead_sort;
mod binary_insertion_sort;
mod bingo_sort;
mod bitonic_sort;
mod bogo_sort;
mod bubble_sort;
mod bucket_sort;
mod cocktail_shaker_sort;
mod comb_sort;
mod counting_sort;
mod cycle_sort;
mod dutch_national_flag_sort;
mod exchange_sort;
mod gnome_sort;
mod heap_sort;
mod insertion_sort;
mod intro_sort;
mod... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/patience_sort.rs | src/sorting/patience_sort.rs | use std::vec;
pub fn patience_sort<T: Ord + Copy>(arr: &mut [T]) {
if arr.is_empty() {
return;
}
// collect piles from arr
let mut piles: Vec<Vec<T>> = Vec::new();
for &card in arr.iter() {
let mut left = 0usize;
let mut right = piles.len();
while left < right {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/pancake_sort.rs | src/sorting/pancake_sort.rs | use std::cmp;
pub fn pancake_sort<T>(arr: &mut [T]) -> Vec<T>
where
T: cmp::PartialEq + cmp::Ord + cmp::PartialOrd + Clone,
{
let len = arr.len();
if len < 2 {
arr.to_vec();
}
for i in (0..len).rev() {
let max_index = arr
.iter()
.take(i + 1)
.enu... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/cycle_sort.rs | src/sorting/cycle_sort.rs | // sorts with the minimum number of rewrites. Runs through all values in the array, placing them in their correct spots. O(n^2).
pub fn cycle_sort(arr: &mut [i32]) {
for cycle_start in 0..arr.len() {
let mut item = arr[cycle_start];
let mut pos = cycle_start;
for i in arr.iter().skip(cycle_... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/binary_insertion_sort.rs | src/sorting/binary_insertion_sort.rs | fn _binary_search<T: Ord>(arr: &[T], target: &T) -> usize {
let mut low = 0;
let mut high = arr.len();
while low < high {
let mid = low + (high - low) / 2;
if arr[mid] < *target {
low = mid + 1;
} else {
high = mid;
}
}
low
}
pub fn binary_... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/sort_utils.rs | src/sorting/sort_utils.rs | use rand::Rng;
use std::time::Instant;
#[cfg(test)]
pub fn generate_random_vec(n: u32, range_l: i32, range_r: i32) -> Vec<i32> {
let mut arr = Vec::<i32>::with_capacity(n as usize);
let mut rng = rand::rng();
let mut count = n;
while count > 0 {
arr.push(rng.random_range(range_l..=range_r));
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/merge_sort.rs | src/sorting/merge_sort.rs | fn merge<T: Ord + Copy>(arr: &mut [T], mid: usize) {
// Create temporary vectors to support the merge.
let left_half = arr[..mid].to_vec();
let right_half = arr[mid..].to_vec();
// Indexes to track the positions while merging.
let mut l = 0;
let mut r = 0;
for v in arr {
// Choose ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/bingo_sort.rs | src/sorting/bingo_sort.rs | use std::cmp::{max, min};
// Function for finding the maximum and minimum element of the Array
fn max_min(vec: &[i32], bingo: &mut i32, next_bingo: &mut i32) {
for &element in vec.iter().skip(1) {
*bingo = min(*bingo, element);
*next_bingo = max(*next_bingo, element);
}
}
pub fn bingo_sort(vec... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/odd_even_sort.rs | src/sorting/odd_even_sort.rs | pub fn odd_even_sort<T: Ord>(arr: &mut [T]) {
let len = arr.len();
if len == 0 {
return;
}
let mut sorted = false;
while !sorted {
sorted = true;
for i in (1..len - 1).step_by(2) {
if arr[i] > arr[i + 1] {
arr.swap(i, i + 1);
sort... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/insertion_sort.rs | src/sorting/insertion_sort.rs | /// Sorts a mutable slice using in-place insertion sort algorithm.
///
/// Time complexity is `O(n^2)`, where `n` is the number of elements.
/// Space complexity is `O(1)` as it sorts elements in-place.
pub fn insertion_sort<T: Ord + Copy>(arr: &mut [T]) {
for i in 1..arr.len() {
let mut j = i;
let ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/exchange_sort.rs | src/sorting/exchange_sort.rs | // sorts through swapping the first value until it is at the right position, and repeating for all the following.
pub fn exchange_sort(arr: &mut [i32]) {
let length = arr.len();
for number1 in 0..length {
for number2 in (number1 + 1)..length {
if arr[number2] < arr[number1] {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/quick_sort.rs | src/sorting/quick_sort.rs | pub fn partition<T: PartialOrd>(arr: &mut [T], lo: usize, hi: usize) -> usize {
let pivot = hi;
let mut i = lo;
let mut j = hi - 1;
loop {
while arr[i] < arr[pivot] {
i += 1;
}
while j > 0 && arr[j] > arr[pivot] {
j -= 1;
}
if j == 0 || i ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/bucket_sort.rs | src/sorting/bucket_sort.rs | /// Sort a slice using bucket sort algorithm.
///
/// Time complexity is `O(n + k)` on average, where `n` is the number of elements,
/// `k` is the number of buckets used in process.
///
/// Space complexity is `O(n + k)`, as it sorts not in-place.
pub fn bucket_sort(arr: &[usize]) -> Vec<usize> {
if arr.is_empty()... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/shell_sort.rs | src/sorting/shell_sort.rs | pub fn shell_sort<T: Ord + Copy>(values: &mut [T]) {
// shell sort works by swiping the value at a given gap and decreasing the gap to 1
fn insertion<T: Ord + Copy>(values: &mut [T], start: usize, gap: usize) {
for i in ((start + gap)..values.len()).step_by(gap) {
let val_current = values[i]... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/intro_sort.rs | src/sorting/intro_sort.rs | // Intro Sort (Also known as Introspective Sort)
// Introspective Sort is hybrid sort (Quick Sort + Heap Sort + Insertion Sort)
// https://en.wikipedia.org/wiki/Introsort
fn insertion_sort<T: Ord>(arr: &mut [T]) {
for i in 1..arr.len() {
let mut j = i;
while j > 0 && arr[j] < arr[j - 1] {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/sorting/bitonic_sort.rs | src/sorting/bitonic_sort.rs | fn _comp_and_swap<T: Ord>(array: &mut [T], left: usize, right: usize, ascending: bool) {
if (ascending && array[left] > array[right]) || (!ascending && array[left] < array[right]) {
array.swap(left, right);
}
}
fn _bitonic_merge<T: Ord>(array: &mut [T], low: usize, length: usize, ascending: bool) {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/palindrome_partitioning.rs | src/dynamic_programming/palindrome_partitioning.rs | /// Finds the minimum cuts needed for a palindrome partitioning of a string
///
/// Given a string s, partition s such that every substring of the partition is a palindrome.
/// This function returns the minimum number of cuts needed.
///
/// Time Complexity: O(n^2)
/// Space Complexity: O(n^2)
///
/// # Arguments
///
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/matrix_chain_multiply.rs | src/dynamic_programming/matrix_chain_multiply.rs | //! This module implements a dynamic programming solution to find the minimum
//! number of multiplications needed to multiply a chain of matrices with given dimensions.
//!
//! The algorithm uses a dynamic programming approach with tabulation to calculate the minimum
//! number of multiplications required for matrix c... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/minimum_cost_path.rs | src/dynamic_programming/minimum_cost_path.rs | use std::cmp::min;
/// Represents possible errors that can occur when calculating the minimum cost path in a matrix.
#[derive(Debug, PartialEq, Eq)]
pub enum MatrixError {
/// Error indicating that the matrix is empty or has empty rows.
EmptyMatrix,
/// Error indicating that the matrix is not rectangular i... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/knapsack.rs | src/dynamic_programming/knapsack.rs | //! This module provides functionality to solve the knapsack problem using dynamic programming.
//! It includes structures for items and solutions, and functions to compute the optimal solution.
use std::cmp::Ordering;
/// Represents an item with a weight and a value.
#[derive(Debug, PartialEq, Eq)]
pub struct Item {... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/longest_increasing_subsequence.rs | src/dynamic_programming/longest_increasing_subsequence.rs | /// Finds the longest increasing subsequence and returns it.
///
/// If multiple subsequences with the longest possible subsequence length can be found, the
/// subsequence which appeared first will be returned (see `test_example_1`).
///
/// Inspired by [this LeetCode problem](https://leetcode.com/problems/longest-inc... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/trapped_rainwater.rs | src/dynamic_programming/trapped_rainwater.rs | //! Module to calculate trapped rainwater in an elevation map.
/// Computes the total volume of trapped rainwater in a given elevation map.
///
/// # Arguments
///
/// * `elevation_map` - A slice containing the heights of the terrain elevations.
///
/// # Returns
///
/// The total volume of trapped rainwater.
pub fn t... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/longest_common_subsequence.rs | src/dynamic_programming/longest_common_subsequence.rs | //! This module implements the Longest Common Subsequence (LCS) algorithm.
//! The LCS problem is finding the longest subsequence common to two sequences.
//! It differs from the problem of finding common substrings: unlike substrings, subsequences
//! are not required to occupy consecutive positions within the origina... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/is_subsequence.rs | src/dynamic_programming/is_subsequence.rs | //! A module for checking if one string is a subsequence of another string.
//!
//! A subsequence is formed by deleting some (can be none) of the characters
//! from the original string without disturbing the relative positions of the
//! remaining characters. This module provides a function to determine if
//! a given... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/longest_continuous_increasing_subsequence.rs | src/dynamic_programming/longest_continuous_increasing_subsequence.rs | use std::cmp::Ordering;
/// Finds the longest continuous increasing subsequence in a slice.
///
/// Given a slice of elements, this function returns a slice representing
/// the longest continuous subsequence where each element is strictly
/// less than the following element.
///
/// # Arguments
///
/// * `arr` - A re... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/subset_sum.rs | src/dynamic_programming/subset_sum.rs | //! This module provides a solution to the subset sum problem using dynamic programming.
//!
//! # Complexity
//! - Time complexity: O(n * sum) where n is array length and sum is the target sum
//! - Space complexity: O(n * sum) for the DP table
/// Determines if there exists a subset of the given array that sums to th... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/longest_common_substring.rs | src/dynamic_programming/longest_common_substring.rs | //! This module provides a function to find the length of the longest common substring
//! between two strings using dynamic programming.
/// Finds the length of the longest common substring between two strings using dynamic programming.
///
/// The algorithm uses a 2D dynamic programming table where each cell represe... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/maximum_subarray.rs | src/dynamic_programming/maximum_subarray.rs | //! This module provides a function to find the largest sum of the subarray
//! in a given array of integers using dynamic programming. It also includes
//! tests to verify the correctness of the implementation.
/// Custom error type for maximum subarray
#[derive(Debug, PartialEq)]
pub enum MaximumSubarrayError {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/smith_waterman.rs | src/dynamic_programming/smith_waterman.rs | //! This module contains the Smith-Waterman algorithm implementation for local sequence alignment.
//!
//! The Smith-Waterman algorithm is a dynamic programming algorithm used for determining
//! similar regions between two sequences (nucleotide or protein sequences). It is particularly
//! useful in bioinformatics for... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/fractional_knapsack.rs | src/dynamic_programming/fractional_knapsack.rs | pub fn fractional_knapsack(mut capacity: f64, weights: Vec<f64>, values: Vec<f64>) -> f64 {
// vector of tuple of weights and their value/weight ratio
let mut weights: Vec<(f64, f64)> = weights
.iter()
.zip(values.iter())
.map(|(&w, &v)| (w, v / w))
.collect();
// sort in de... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/task_assignment.rs | src/dynamic_programming/task_assignment.rs | // Task Assignment Problem using Bitmasking and DP in Rust
// Time Complexity: O(2^M * N) where M is number of people and N is number of tasks
// Space Complexity: O(2^M * N) for the DP table
use std::collections::HashMap;
/// Solves the task assignment problem where each person can do only certain tasks,
/// each pe... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/integer_partition.rs | src/dynamic_programming/integer_partition.rs | //! Integer partition using dynamic programming
//!
//! The number of partitions of a number n into at least k parts equals the number of
//! partitions into exactly k parts plus the number of partitions into at least k-1 parts.
//! Subtracting 1 from each part of a partition of n into k parts gives a partition of n-k
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/snail.rs | src/dynamic_programming/snail.rs | /// ## Spiral Sorting
///
/// Given an n x m array, return the array elements arranged from outermost elements
/// to the middle element, traveling INWARD FROM TOP-LEFT, CLOCKWISE.
pub fn snail<T: Copy>(matrix: &[Vec<T>]) -> Vec<T> {
// break on empty matrix
if matrix.is_empty() || matrix[0].is_empty() {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/catalan_numbers.rs | src/dynamic_programming/catalan_numbers.rs | //! Catalan Numbers using Dynamic Programming
//!
//! The Catalan numbers are a sequence of positive integers that appear in many
//! counting problems in combinatorics. Such problems include counting:
//! - The number of Dyck words of length 2n
//! - The number of well-formed expressions with n pairs of parentheses
//... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/optimal_bst.rs | src/dynamic_programming/optimal_bst.rs | // Optimal Binary Search Tree Algorithm in Rust
// Time Complexity: O(n^3) with prefix sum optimization
// Space Complexity: O(n^2) for the dp table and prefix sum array
/// Constructs an Optimal Binary Search Tree from a list of key frequencies.
/// The goal is to minimize the expected search cost given key access fr... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/mod.rs | src/dynamic_programming/mod.rs | mod catalan_numbers;
mod coin_change;
mod egg_dropping;
mod fibonacci;
mod fractional_knapsack;
mod integer_partition;
mod is_subsequence;
mod knapsack;
mod longest_common_subsequence;
mod longest_common_substring;
mod longest_continuous_increasing_subsequence;
mod longest_increasing_subsequence;
mod matrix_chain_multi... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/egg_dropping.rs | src/dynamic_programming/egg_dropping.rs | //! This module contains the `egg_drop` function, which determines the minimum number of egg droppings
//! required to find the highest floor from which an egg can be dropped without breaking. It also includes
//! tests for the function using various test cases, including edge cases.
/// Returns the least number of eg... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/fibonacci.rs | src/dynamic_programming/fibonacci.rs | /// Fibonacci via Dynamic Programming
use std::collections::HashMap;
/// fibonacci(n) returns the nth fibonacci number
/// This function uses the definition of Fibonacci where:
/// F(0) = F(1) = 1 and F(n+1) = F(n) + F(n-1) for n>0
///
/// Warning: This will overflow the 128-bit unsigned integer at n=186
pub fn fibona... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/maximal_square.rs | src/dynamic_programming/maximal_square.rs | use std::cmp::max;
use std::cmp::min;
/// Maximal Square
///
/// Given an `m` * `n` binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.\
/// <https://leetcode.com/problems/maximal-square/>
///
/// # Arguments:
/// * `matrix` - an array of integer array
///
/// # Co... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/rod_cutting.rs | src/dynamic_programming/rod_cutting.rs | //! This module provides functions for solving the rod-cutting problem using dynamic programming.
use std::cmp::max;
/// Calculates the maximum possible profit from cutting a rod into pieces of varying lengths.
///
/// Returns the maximum profit achievable by cutting a rod into pieces such that the profit from each
//... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/word_break.rs | src/dynamic_programming/word_break.rs | use crate::data_structures::Trie;
/// Checks if a string can be segmented into a space-separated sequence
/// of one or more words from the given dictionary.
///
/// # Arguments
/// * `s` - The input string to be segmented.
/// * `word_dict` - A slice of words forming the dictionary.
///
/// # Returns
/// * `bool` - `... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/subset_generation.rs | src/dynamic_programming/subset_generation.rs | // list all subset combinations of n element in given set of r element.
// This is a recursive function that collects all subsets of the set of size n
// with the given set of size r.
pub fn list_subset(
set: &[i32],
n: usize,
r: usize,
index: usize,
data: &mut [i32],
i: usize,
) -> Vec<Vec<i32>... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/dynamic_programming/coin_change.rs | src/dynamic_programming/coin_change.rs | //! This module provides a solution to the coin change problem using dynamic programming.
//! The `coin_change` function calculates the fewest number of coins required to make up
//! a given amount using a specified set of coin denominations.
//!
//! The implementation leverages dynamic programming to build up solution... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/k_means.rs | src/machine_learning/k_means.rs | use rand::random;
fn get_distance(p1: &(f64, f64), p2: &(f64, f64)) -> f64 {
let dx: f64 = p1.0 - p2.0;
let dy: f64 = p1.1 - p2.1;
((dx * dx) + (dy * dy)).sqrt()
}
fn find_nearest(data_point: &(f64, f64), centroids: &[(f64, f64)]) -> u32 {
let mut cluster: u32 = 0;
for (i, c) in centroids.iter()... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/linear_regression.rs | src/machine_learning/linear_regression.rs | /// Returns the parameters of the line after performing simple linear regression on the input data.
pub fn linear_regression(data_points: Vec<(f64, f64)>) -> Option<(f64, f64)> {
if data_points.is_empty() {
return None;
}
let count = data_points.len() as f64;
let mean_x = data_points.iter().fol... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/mod.rs | src/machine_learning/mod.rs | mod cholesky;
mod k_means;
mod linear_regression;
mod logistic_regression;
mod loss_function;
mod optimization;
pub use self::cholesky::cholesky;
pub use self::k_means::k_means;
pub use self::linear_regression::linear_regression;
pub use self::logistic_regression::logistic_regression;
pub use self::loss_function::aver... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/logistic_regression.rs | src/machine_learning/logistic_regression.rs | use super::optimization::gradient_descent;
use std::f64::consts::E;
/// Returns the weights after performing Logistic regression on the input data points.
pub fn logistic_regression(
data_points: Vec<(Vec<f64>, f64)>,
iterations: usize,
learning_rate: f64,
) -> Option<Vec<f64>> {
if data_points.is_empt... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/cholesky.rs | src/machine_learning/cholesky.rs | pub fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
if (mat.is_empty()) || (n == 0) {
return vec![];
}
let mut res = vec![0.0; mat.len()];
for i in 0..n {
for j in 0..=i {
let mut s = 0.0;
for k in 0..j {
s += res[i * n + k] * res[j * n + k];
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/optimization/momentum.rs | src/machine_learning/optimization/momentum.rs | /// Momentum Optimization
///
/// Momentum is an extension of gradient descent that accelerates convergence by accumulating
/// a velocity vector in directions of persistent reduction in the objective function.
/// This helps the optimizer navigate ravines and avoid getting stuck in local minima.
///
/// The algorithm ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/optimization/adam.rs | src/machine_learning/optimization/adam.rs | //! # Adam (Adaptive Moment Estimation) optimizer
//!
//! The `Adam (Adaptive Moment Estimation)` optimizer is an adaptive learning rate algorithm used
//! in gradient descent and machine learning, such as for training neural networks to solve deep
//! learning problems. Boasting memory-efficient fast convergence rates... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/optimization/gradient_descent.rs | src/machine_learning/optimization/gradient_descent.rs | /// Gradient Descent Optimization
///
/// Gradient descent is an iterative optimization algorithm used to find the minimum of a function.
/// It works by updating the parameters (in this case, elements of the vector `x`) in the direction of
/// the steepest decrease in the function's value. This is achieved by subtract... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/optimization/mod.rs | src/machine_learning/optimization/mod.rs | mod adam;
mod gradient_descent;
mod momentum;
pub use self::adam::Adam;
pub use self::gradient_descent::gradient_descent;
| rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/loss_function/negative_log_likelihood.rs | src/machine_learning/loss_function/negative_log_likelihood.rs | // Negative Log Likelihood Loss Function
//
// The `neg_log_likelihood` function calculates the Negative Log Likelyhood loss,
// which is a loss function used for classification problems in machine learning.
//
// ## Formula
//
// For a pair of actual and predicted values, represented as vectors `y_true` and
// `y_pred... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/loss_function/kl_divergence_loss.rs | src/machine_learning/loss_function/kl_divergence_loss.rs | //! # KL divergence Loss Function
//!
//! For a pair of actual and predicted probability distributions represented as vectors `actual` and `predicted`, the KL divergence loss is calculated as:
//!
//! `L = -Σ(actual[i] * ln(predicted[i]/actual[i]))` for all `i` in the range of the vectors
//!
//! Where `ln` is the natu... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/loss_function/huber_loss.rs | src/machine_learning/loss_function/huber_loss.rs | /// Computes the Huber loss between arrays of true and predicted values.
///
/// # Arguments
///
/// * `y_true` - An array of true values.
/// * `y_pred` - An array of predicted values.
/// * `delta` - The threshold parameter that controls the linear behavior of the loss function.
///
/// # Returns
///
/// The average ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/loss_function/mean_absolute_error_loss.rs | src/machine_learning/loss_function/mean_absolute_error_loss.rs | //! # Mean Absolute Error Loss Function
//!
//! The `mae_loss` function calculates the Mean Absolute Error loss, which is a
//! robust loss function used in machine learning.
//!
//! ## Formula
//!
//! For a pair of actual and predicted values, represented as vectors `actual`
//! and `predicted`, the Mean Absolute los... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/loss_function/hinge_loss.rs | src/machine_learning/loss_function/hinge_loss.rs | //! # Hinge Loss
//!
//! The `hng_loss` function calculates the Hinge loss, which is a
//! loss function used for classification problems in machine learning.
//!
//! ## Formula
//!
//! For a pair of actual and predicted values, represented as vectors `y_true` and
//! `y_pred`, the Hinge loss is calculated as:
//!
//! ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/loss_function/average_margin_ranking_loss.rs | src/machine_learning/loss_function/average_margin_ranking_loss.rs | /// Marginal Ranking
///
/// The 'average_margin_ranking_loss' function calculates the Margin Ranking loss, which is a
/// loss function used for ranking problems in machine learning.
///
/// ## Formula
///
/// For a pair of values `x_first` and `x_second`, `margin`, and `y_true`,
/// the Margin Ranking loss is calcula... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/loss_function/mod.rs | src/machine_learning/loss_function/mod.rs | mod average_margin_ranking_loss;
mod hinge_loss;
mod huber_loss;
mod kl_divergence_loss;
mod mean_absolute_error_loss;
mod mean_squared_error_loss;
mod negative_log_likelihood;
pub use self::average_margin_ranking_loss::average_margin_ranking_loss;
pub use self::hinge_loss::hng_loss;
pub use self::huber_loss::huber_lo... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/machine_learning/loss_function/mean_squared_error_loss.rs | src/machine_learning/loss_function/mean_squared_error_loss.rs | //! # Mean Square Loss Function
//!
//! The `mse_loss` function calculates the Mean Square Error loss, which is a
//! robust loss function used in machine learning.
//!
//! ## Formula
//!
//! For a pair of actual and predicted values, represented as vectors `actual`
//! and `predicted`, the Mean Square loss is calcula... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/union_find.rs | src/data_structures/union_find.rs | //! A Union-Find (Disjoint Set) data structure implementation in Rust.
//!
//! The Union-Find data structure keeps track of elements partitioned into
//! disjoint (non-overlapping) sets.
//! It provides near-constant-time operations to add new sets, to find the
//! representative of a set, and to merge sets.
use std::... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/floyds_algorithm.rs | src/data_structures/floyds_algorithm.rs | // floyds_algorithm.rs
// https://github.com/rust-lang/rust/blob/master/library/alloc/src/collections/linked_list.rs#L113
// use std::collections::linked_list::LinkedList;
// https://www.reddit.com/r/rust/comments/t7wquc/is_it_possible_to_solve_leetcode_problem141/
use crate::data_structures::linked_list::LinkedList; ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/stack_using_singly_linked_list.rs | src/data_structures/stack_using_singly_linked_list.rs | // The public struct can hide the implementation detail
pub struct Stack<T> {
head: Link<T>,
}
type Link<T> = Option<Box<Node<T>>>;
struct Node<T> {
elem: T,
next: Link<T>,
}
impl<T> Stack<T> {
// Self is an alias for Stack
// We implement associated function name new for single-linked-list
p... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/treap.rs | src/data_structures/treap.rs | use std::{
cmp::Ordering,
iter::FromIterator,
mem,
ops::Not,
time::{SystemTime, UNIX_EPOCH},
};
/// An internal node of an `Treap`.
struct TreapNode<T: Ord> {
value: T,
priority: usize,
left: Option<Box<TreapNode<T>>>,
right: Option<Box<TreapNode<T>>>,
}
/// A set based on a Treap ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/b_tree.rs | src/data_structures/b_tree.rs | use std::convert::TryFrom;
use std::fmt::Debug;
use std::mem;
struct Node<T> {
keys: Vec<T>,
children: Vec<Node<T>>,
}
pub struct BTree<T> {
root: Node<T>,
props: BTreeProps,
}
// Why to need a different Struct for props...
// Check - http://smallcultfollowing.com/babysteps/blog/2018/11/01/after-nll-... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/binary_search_tree.rs | src/data_structures/binary_search_tree.rs | use std::cmp::Ordering;
use std::ops::Deref;
/// This struct implements as Binary Search Tree (BST), which is a
/// simple data structure for storing sorted data
pub struct BinarySearchTree<T>
where
T: Ord,
{
value: Option<T>,
left: Option<Box<BinarySearchTree<T>>>,
right: Option<Box<BinarySearchTree<T... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/range_minimum_query.rs | src/data_structures/range_minimum_query.rs | //! Range Minimum Query (RMQ) Implementation
//!
//! This module provides an efficient implementation of a Range Minimum Query data structure using a
//! sparse table approach. It allows for quick retrieval of the minimum value within a specified subdata
//! of a given data after an initial preprocessing phase.
//!
//!... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/mod.rs | src/data_structures/mod.rs | mod avl_tree;
mod b_tree;
mod binary_search_tree;
mod fenwick_tree;
mod floyds_algorithm;
pub mod graph;
mod hash_table;
mod heap;
mod lazy_segment_tree;
mod linked_list;
mod probabilistic;
mod queue;
mod range_minimum_query;
mod rb_tree;
mod segment_tree;
mod segment_tree_recursive;
mod skip_list;
mod stack_using_sing... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/rb_tree.rs | src/data_structures/rb_tree.rs | use std::boxed::Box;
use std::cmp::{Ord, Ordering};
use std::iter::Iterator;
use std::ptr::null_mut;
#[derive(Copy, Clone)]
enum Color {
Red,
Black,
}
pub struct RBNode<K: Ord, V> {
key: K,
value: V,
color: Color,
parent: *mut RBNode<K, V>,
left: *mut RBNode<K, V>,
right: *mut RBNode<K... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/linked_list.rs | src/data_structures/linked_list.rs | use std::fmt::{self, Display, Formatter};
use std::marker::PhantomData;
use std::ptr::NonNull;
pub struct Node<T> {
pub val: T,
pub next: Option<NonNull<Node<T>>>,
prev: Option<NonNull<Node<T>>>,
}
impl<T> Node<T> {
fn new(t: T) -> Node<T> {
Node {
val: t,
prev: None,
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/hash_table.rs | src/data_structures/hash_table.rs | use std::collections::LinkedList;
pub struct HashTable<K, V> {
elements: Vec<LinkedList<(K, V)>>,
count: usize,
}
impl<K: Hashable + std::cmp::PartialEq, V> Default for HashTable<K, V> {
fn default() -> Self {
Self::new()
}
}
pub trait Hashable {
fn hash(&self) -> usize;
}
impl<K: Hashab... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/segment_tree.rs | src/data_structures/segment_tree.rs | //! A module providing a Segment Tree data structure for efficient range queries
//! and updates. It supports operations like finding the minimum, maximum,
//! and sum of segments in an array.
use std::fmt::Debug;
use std::ops::Range;
/// Custom error types representing possible errors that can occur during operation... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/lazy_segment_tree.rs | src/data_structures/lazy_segment_tree.rs | use std::fmt::{Debug, Display};
use std::ops::{Add, AddAssign, Range};
pub struct LazySegmentTree<T: Debug + Default + Ord + Copy + Display + AddAssign + Add<Output = T>>
{
len: usize,
tree: Vec<T>,
lazy: Vec<Option<T>>,
merge: fn(T, T) -> T,
}
impl<T: Debug + Default + Ord + Copy + Display + AddAssig... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/veb_tree.rs | src/data_structures/veb_tree.rs | // This struct implements Van Emde Boas tree (VEB tree). It stores integers in range [0, U), where
// O is any integer that is a power of 2. It supports operations such as insert, search,
// predecessor, and successor in O(log(log(U))) time. The structure takes O(U) space.
pub struct VebTree {
size: u32,
child_... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/graph.rs | src/data_structures/graph.rs | use std::collections::{HashMap, HashSet};
use std::fmt;
#[derive(Debug, Clone)]
pub struct NodeNotInGraph;
impl fmt::Display for NodeNotInGraph {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "accessing a node that is not in the graph")
}
}
pub struct DirectedGraph {
adjacency_... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/avl_tree.rs | src/data_structures/avl_tree.rs | use std::{
cmp::{max, Ordering},
iter::FromIterator,
mem,
ops::Not,
};
/// An internal node of an `AVLTree`.
struct AVLNode<T: Ord> {
value: T,
height: usize,
left: Option<Box<AVLNode<T>>>,
right: Option<Box<AVLNode<T>>>,
}
/// A set based on an AVL Tree.
///
/// An AVL Tree is a self-... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/fenwick_tree.rs | src/data_structures/fenwick_tree.rs | use std::ops::{Add, AddAssign, Sub, SubAssign};
/// A Fenwick Tree (also known as a Binary Indexed Tree) that supports efficient
/// prefix sum, range sum and point queries, as well as point updates.
///
/// The Fenwick Tree uses **1-based** indexing internally but presents a **0-based** interface to the user.
/// Thi... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/queue.rs | src/data_structures/queue.rs | //! This module provides a generic `Queue` data structure, implemented using
//! Rust's `LinkedList` from the standard library. The queue follows the FIFO
//! (First-In-First-Out) principle, where elements are added to the back of
//! the queue and removed from the front.
use std::collections::LinkedList;
#[derive(De... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/trie.rs | src/data_structures/trie.rs | //! This module provides a generic implementation of a Trie (prefix tree).
//! A Trie is a tree-like data structure that is commonly used to store sequences of keys
//! (such as strings, integers, or other iterable types) where each node represents one element
//! of the key, and values can be associated with full sequ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/segment_tree_recursive.rs | src/data_structures/segment_tree_recursive.rs | use std::fmt::Debug;
use std::ops::Range;
/// Custom error types representing possible errors that can occur during operations on the `SegmentTree`.
#[derive(Debug, PartialEq, Eq)]
pub enum SegmentTreeError {
/// Error indicating that an index is out of bounds.
IndexOutOfBounds,
/// Error indicating that a... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/skip_list.rs | src/data_structures/skip_list.rs | use rand::random_range;
use std::{cmp::Ordering, marker::PhantomData, ptr::null_mut};
struct Node<K: Ord, V> {
key: Option<K>,
value: Option<V>,
forward: Vec<*mut Node<K, V>>,
}
impl<K: Ord, V> Node<K, V> {
pub fn new() -> Self {
let mut forward = Vec::with_capacity(4);
forward.resize(... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/heap.rs | src/data_structures/heap.rs | //! A generic heap data structure.
//!
//! This module provides a `Heap` implementation that can function as either a
//! min-heap or a max-heap. It supports common heap operations such as adding,
//! removing, and iterating over elements. The heap can also be created from
//! an unsorted vector and supports custom com... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/probabilistic/mod.rs | src/data_structures/probabilistic/mod.rs | pub mod bloom_filter;
pub mod count_min_sketch;
| rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/probabilistic/bloom_filter.rs | src/data_structures/probabilistic/bloom_filter.rs | use std::collections::hash_map::{DefaultHasher, RandomState};
use std::hash::{BuildHasher, Hash, Hasher};
/// A Bloom Filter <https://en.wikipedia.org/wiki/Bloom_filter> is a probabilistic data structure testing whether an element belongs to a set or not
/// Therefore, its contract looks very close to the one of a set... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/data_structures/probabilistic/count_min_sketch.rs | src/data_structures/probabilistic/count_min_sketch.rs | use std::collections::hash_map::RandomState;
use std::fmt::{Debug, Formatter};
use std::hash::{BuildHasher, Hash};
/// A probabilistic data structure holding an approximate count for diverse items efficiently (using constant space)
///
/// Let's imagine we want to count items from an incoming (unbounded) data stream
/... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.