算法在计算机科学和程序设计中扮演着至关重要的角色,如在解决问题、优化效率、决策优化、实现计算机程序、提高可靠性以及促进科学融合等方面具有广泛而深远的影响。今天大姚给大家分享一个开源、免费、全面的C#算法实战教程:TheAlgorithms/C-Sharp。
一个C#实现的各种算法集合,这些算法涵盖了计算机科学、数学和统计学、数据科学、机器学习、工程等多个领域。这些实现及其相关文档旨在为教育工作者和学生提供学习资源。因此,可能会找到针对同一目标使用不同算法策略和优化的多种实现。
图片
/// <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.28at.com/showinfo-26-92118-0.html一个开源且全面的C#算法实战教程
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com
上一篇: 快看,我的代码能“自己说话”!