自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一個開源且全面的C#算法實戰(zhàn)教程

開發(fā) 前端
一個C#實現的各種算法集合,這些算法涵蓋了計算機科學、數學和統計學、數據科學、機器學習、工程等多個領域。這些實現及其相關文檔旨在為教育工作者和學生提供學習資源。

前言

算法在計算機科學和程序設計中扮演著至關重要的角色,如在解決問題、優(yōu)化效率、決策優(yōu)化、實現計算機程序、提高可靠性以及促進科學融合等方面具有廣泛而深遠的影響。今天大姚給大家分享一個開源、免費、全面的C#算法實戰(zhàn)教程:TheAlgorithms/C-Sharp。

項目介紹

一個C#實現的各種算法集合,這些算法涵蓋了計算機科學、數學和統計學、數據科學、機器學習、工程等多個領域。這些實現及其相關文檔旨在為教育工作者和學生提供學習資源。因此,可能會找到針對同一目標使用不同算法策略和優(yōu)化的多種實現。

項目源代碼

圖片圖片

主要算法包括

  • 排序算法:冒泡排序、插入排序、計數排序、快速排序等
  • 搜索算法:線性搜索、二分搜索等
  • 數值計算:最大公約數、二項式系數、牛頓的平方根計算、歐拉方法等
  • 字符串算法:Rabin-Karp 算法、KMP 算法、Manacher 算法等
  • 數據結構:鏈表 (Linked List)、棧 (Stack)、隊列 (Queue)、二叉樹 (Binary Tree)等
  • 圖算法:深度優(yōu)先搜索 (Depth-First Search)、廣度優(yōu)先搜索 (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;
    }
}

項目源碼地址

更多項目實用功能和特性歡迎前往項目開源地址查看??,別忘了給項目一個Star支持??。

GitHub開源地址:https://github.com/TheAlgorithms/C-Sharp

責任編輯:武曉燕 來源: 追逐時光者
相關推薦

2009-07-22 17:15:04

C#實現

2009-09-08 17:37:54

C# listbox控

2017-10-18 15:19:23

架構師技術開發(fā)

2009-08-19 14:15:42

C# 復合控件

2024-12-26 00:14:45

C#腳本開源

2009-07-30 18:18:27

C#時間計算

2009-08-18 17:19:33

C#事件模型

2009-09-11 09:11:09

2014-03-12 10:42:10

equeue分布式消息隊列

2009-07-31 17:14:19

C#語言Web程序

2018-11-09 09:40:52

2009-08-25 01:46:00

C# WINDOWS服

2009-08-26 15:53:42

C#數據訪問XML

2019-09-03 09:41:48

運維架構技術

2019-04-09 08:50:15

Rancher容器運維

2009-09-01 10:20:28

C#多種語句

2009-08-11 14:57:11

比較C#和Java

2009-09-18 10:45:31

C#數組操作

2009-07-31 15:52:47

C#常用函數

2020-04-10 10:15:29

算法開源Github
點贊
收藏

51CTO技術棧公眾號