代码拉取完成,页面将自动刷新
同步操作将从 编程语言算法集/C-Sharp 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
using System;
using System.Linq;
namespace Algorithms.Sorters.Integer
{
/// <summary>
/// Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that
/// is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key
/// value, and using arithmetic on those counts to determine the positions of each key value in the output sequence.
/// Its running time is linear in the number of items and the difference between the maximum and minimum key values, so
/// it is only suitable for direct use in situations where the variation in keys is not significantly greater than the
/// number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can
/// handle larger keys more efficiently.
/// </summary>
public class CountingSorter : IIntegerSorter
{
/// <summary>
/// <para>
/// Sorts input array using counting sort algorithm.
/// </para>
/// <para>
/// Time complexity: O(n+k), where k is the range of the non-negative key values.
/// </para>
/// <para>
/// Space complexity: O(n+k), where k is the range of the non-negative key values.
/// </para>
/// </summary>
/// <param name="array">Input array.</param>
public void Sort(int[] array)
{
if (array.Length == 0)
{
return;
}
var max = array.Max();
var min = array.Min();
var count = new int[max - min + 1];
var output = new int[array.Length];
for (var i = 0; i < array.Length; i++)
{
count[array[i] - min]++;
}
for (var i = 1; i < count.Length; i++)
{
count[i] += count[i - 1];
}
for (var i = array.Length - 1; i >= 0; i--)
{
output[count[array[i] - min] - 1] = array[i];
count[array[i] - min]--;
}
Array.Copy(output, array, array.Length);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。