算法在計算機科學(xué)和程序設(shè)計中扮演著至關(guān)重要的角色,如在解決問題、優(yōu)化效率、決策優(yōu)化、實現(xiàn)計算機程序、提高可靠性以及促進(jìn)科學(xué)融合等方面具有廣泛而深遠(yuǎn)的影響。今天大姚給大家分享一個開源、免費、全面的C#算法實戰(zhàn)教程:TheAlgorithms/C-Sharp。
一個C#實現(xiàn)的各種算法集合,這些算法涵蓋了計算機科學(xué)、數(shù)學(xué)和統(tǒng)計學(xué)、數(shù)據(jù)科學(xué)、機器學(xué)習(xí)、工程等多個領(lǐng)域。這些實現(xiàn)及其相關(guān)文檔旨在為教育工作者和學(xué)生提供學(xué)習(xí)資源。因此,可能會找到針對同一目標(biāo)使用不同算法策略和優(yōu)化的多種實現(xiàn)。
圖片
/// <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; }}
更多項目實用功能和特性歡迎前往項目開源地址查看
本文鏈接:http://www.tebozhan.com/showinfo-26-92118-0.html一個開源且全面的C#算法實戰(zhàn)教程
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 快看,我的代碼能“自己說話”!