首页 > 编程语言 >一个开源且全面的C#算法实战教程

一个开源且全面的C#算法实战教程

时间:2024-06-12 21:34:51浏览次数:14  
标签:教程 C# 算法 complexity 开源 comparer var array

前言

算法在计算机科学和程序设计中扮演着至关重要的角色,如在解决问题、优化效率、决策优化、实现计算机程序、提高可靠性以及促进科学融合等方面具有广泛而深远的影响。今天大姚给大家分享一个开源、免费、全面的C#算法实战教程:TheAlgorithms/C-Sharp

项目介绍

一个C#实现的各种算法集合,这些算法涵盖了计算机科学、数学和统计学、数据科学、机器学习、工程等多个领域。这些实现及其相关文档旨在为教育工作者和学生提供学习资源。因此,可能会找到针对同一目标使用不同算法策略和优化的多种实现。

项目源代码

主要算法包括

  • 排序算法:冒泡排序、插入排序、计数排序、快速排序等
  • 搜索算法:线性搜索、二分搜索等
  • 数值计算:最大公约数、二项式系数、牛顿的平方根计算、欧拉方法等
  • 字符串算法:Rabin-Karp 算法、KMP 算法、Manacher 算法等
  • 数据结构:链表 (Linked List)、栈 (Stack)、队列 (Queue)、二叉树 (Binary Tree)等
  • 图算法:深度优先搜索 (Depth-First Search)、广度优先搜索 (Breadth-First Search)、Dijkstra 最短路径等
  • 等等......

插入排序

/// <summary>
///     Class that implements insertion sort algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class InsertionSorter<T> : IComparisonSorter<T>
{
    /// <summary>
    ///     Sorts array using specified comparer,
    ///     internal, in-place, stable,
    ///     time complexity: O(n^2),
    ///     space complexity: O(1),
    ///     where n - array length.
    /// </summary>
    /// <param name="array">Array to sort.</param>
    /// <param name="comparer">Compares elements.</param>
    public void Sort(T[] array, IComparer<T> comparer)
    {
        for (var i = 1; i < array.Length; i++)
        {
            for (var j = i; j > 0 && comparer.Compare(array[j], array[j - 1]) < 0; j--)
            {
                var temp = array[j - 1];
                array[j - 1] = array[j];
                array[j] = temp;
            }
        }
    }
}

快速排序

/// <summary>
///     Sorts arrays using quicksort.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public abstract class QuickSorter<T> : IComparisonSorter<T>
{
    /// <summary>
    ///     Sorts array using Hoare partition scheme,
    ///     internal, in-place,
    ///     time complexity average: O(n log(n)),
    ///     time complexity worst: O(n^2),
    ///     space complexity: O(log(n)),
    ///     where n - array length.
    /// </summary>
    /// <param name="array">Array to sort.</param>
    /// <param name="comparer">Compares elements.</param>
    public void Sort(T[] array, IComparer<T> comparer) => Sort(array, comparer, 0, array.Length - 1);

    protected abstract T SelectPivot(T[] array, IComparer<T> comparer, int left, int right);

    private void Sort(T[] array, IComparer<T> comparer, int left, int right)
    {
        if (left >= right)
        {
            return;
        }

        var p = Partition(array, comparer, left, right);
        Sort(array, comparer, left, p);
        Sort(array, comparer, p + 1, right);
    }

    private int Partition(T[] array, IComparer<T> comparer, int left, int right)
    {
        var pivot = SelectPivot(array, comparer, left, right);
        var nleft = left;
        var nright = right;
        while (true)
        {
            while (comparer.Compare(array[nleft], pivot) < 0)
            {
                nleft++;
            }

            while (comparer.Compare(array[nright], pivot) > 0)
            {
                nright--;
            }

            if (nleft >= nright)
            {
                return nright;
            }

            var t = array[nleft];
            array[nleft] = array[nright];
            array[nright] = t;

            nleft++;
            nright--;
        }
    }
}

线性搜索

/// <summary>
///     Class that implements linear search algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class LinearSearcher<T>
{
    /// <summary>
    ///     Finds first item in array that satisfies specified term
    ///     Time complexity: O(n)
    ///     Space complexity: O(1).
    /// </summary>
    /// <param name="data">Array to search in.</param>
    /// <param name="term">Term to check against.</param>
    /// <returns>First item that satisfies term.</returns>
    public T Find(T[] data, Func<T, bool> term)
    {
        for (var i = 0; i < data.Length; i++)
        {
            if (term(data[i]))
            {
                return data[i];
            }
        }

        throw new ItemNotFoundException();
    }

    /// <summary>
    ///     Finds index of first item in array that satisfies specified term
    ///     Time complexity: O(n)
    ///     Space complexity: O(1).
    /// </summary>
    /// <param name="data">Array to search in.</param>
    /// <param name="term">Term to check against.</param>
    /// <returns>Index of first item that satisfies term or -1 if none found.</returns>
    public int FindIndex(T[] data, Func<T, bool> term)
    {
        for (var i = 0; i < data.Length; i++)
        {
            if (term(data[i]))
            {
                return i;
            }
        }

        return -1;
    }
}

项目源码地址

更多项目实用功能和特性欢迎前往项目开源地址查看

标签:教程,C#,算法,complexity,开源,comparer,var,array
From: https://www.cnblogs.com/Can-daydayup/p/18244728

相关文章

  • easyExcel判断合并单元格
    开发中遇到需求:1、查询一组单元格是否是一个合并单元格2、判断指定的单元格是否属于合并单元格区域之前用了POI的原生方法,但是excel太大,频繁的循环导致时间花费太长,因此打算改用easyExcel看看时间会不会少点。上网看了一下,easyExcel没有直接判断合并的方法,需要自己写。以下是......
  • 过滤条件之分组 group by、having、distinct、order by、limit、正则、多表查询和子查
    【一】过滤条件之分组groupby【1】引入--按照指定条件对所有数据进行分组--对员工进行分组按照年龄/部门--...select*from*where*groupby*;【2】按照部门分组(1)查询数据select*fromempgroupbypost;#第一次使用部门分组会报错mysql>select*f......
  • NLP实战入门——文本分类任务(TextRNN,TextCNN,TextRNN_Att,TextRCNN,FastText,DPCNN,BERT,ERN
    本文参考自https://github.com/649453932/Chinese-Text-Classification-Pytorch?tab=readme-ov-file,https://github.com/leerumor/nlp_tutorial?tab=readme-ov-file,https://zhuanlan.zhihu.com/p/73176084,是为了进行NLP的一些典型模型的总结和尝试。中文数据集从THUCNews......
  • Navicat的使用、PDManer、PyMySQL(连接数据库、执行SQL语句、结果获取、插入数据、删
    【一】Navicat的使用【1】连接数据库打开Navicat,找到连接选项输入连接参数测试参数【2】创建数据库新建数据库输入数据库参数新建表并设置主键修改表字段【3】导出SQL文件【4】备份数据库【5】视图展示会展示当前数据库下的所有表模型......
  • C# winform中RDLC报表绘制
    C#winform中RDLC报表绘制使用集成开发环境为VS2010,框架版本为.NETFramework4以下我们以一个简单的学生报表作为例子。publicclassStudent{publicstringname{get;set;}publicstringid{get;set;}publicstringclasses{get;set;}publicstringhome{get;set;}publi......
  • 基于imx6ull_pro中qtcreator环境搭建
    目录(一)说明(二)qt_creator安装(三)qt_creator配置与环境搭建(四)qt_creator所遇问题(一)说明1使用版本Descriptionubuntu18.04.2LTSRelease......
  • 【ARM Coresight Debug 系列 -- ARMv8/v9 Watchpoint 软件实现地址监控详细介绍】
    请阅读【嵌入式开发学习必备专栏】文章目录ARMv8/v9WatchpointexceptionsWatchpoint配置信息读取ExecutionconditionsWatchpointdataaddresscomparisonsSizeofthedataaccessWatchpoint软件配置流程WatchpointType使用介绍WT,Bit[20]:WatchpointType......
  • 论文解读——CVPR2024《Learning by Correction: Efficient Tuning Task for Zero-Sho
    一、研究背景  视觉-语言模型是一类能够处理和理解图像及其相关文本信息的模型,它们在多种视觉-语言任务中展示了卓越的性能。这些任务包括图像描述(imagecaptioning)、视觉问题回答(visualquestionanswering)、图像-文本检索(image-textretrieval)等。这些模型通常经过大规......
  • 论文解读——AAMAS2024《OPEx: A Large Language Model-Powered Framework for Embodi
    一、研究背景  具身指令执行(EmbodiedInstructionFollowing,EIF)是指在一个特定的物理或虚拟环境中,使能自主代理(如机器人或虚拟代理)根据自然语言指令来执行复杂的任务。这种研究领域集中于探索自然语言理解与机器执行能力的结合,尤其是在模拟家庭或日常环境中,如何使代理......
  • SpringMVC
    文章目录1.MVC架构2.基于 Servlet 的MVC模式3.Model1模式与Model2模式Model1模式:Model2模式:4.MVVM架构5.SpringMVC6.SpringMVC实现步骤8.Controller的两种实现方式9.RestFul风格10.@ResponseBody11.乱码解决12.过滤器VS拦截器区别:参考......