1 Star 0 Fork 0

jobily/TheAlgorithms-C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ExchangeSorter.cs 1007 Bytes
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2024-01-05 20:45 . Switch to file-scoped namespaces (#433)
using System.Collections.Generic;
namespace Algorithms.Sorters.Comparison;
/// <summary>
/// Class that implements exchange sort algorithm.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class ExchangeSorter<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 = 0; i < array.Length - 1; i++)
{
for (var j = i + 1; j < array.Length; j++)
{
if (comparer.Compare(array[i], array[j]) > 0)
{
(array[j], array[i]) = (array[i], array[j]);
}
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hubo/the-algorithms-c-sharp.git
git@gitee.com:hubo/the-algorithms-c-sharp.git
hubo
the-algorithms-c-sharp
TheAlgorithms-C-Sharp
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385