代码拉取完成,页面将自动刷新
using System.Collections.Generic;
using System.Linq;
namespace Algorithms.Sorting
{
/// <summary>
/// Only support IList<int> Sort
/// Also called CountSort (not CountingSort)
/// </summary>
public static class PigeonHoleSorter
{
public static void PigeonHoleSort(this IList<int> collection)
{
collection.PigeonHoleSortAscending();
}
public static void PigeonHoleSortAscending(this IList<int> collection)
{
int min = collection.Min();
int max = collection.Max();
int size = max - min + 1;
int[] holes = new int[size];
foreach (int x in collection)
{
holes[x - min]++;
}
int i = 0;
for (int count = 0; count < size; count++)
{
while (holes[count]-- > 0)
{
collection[i] = count + min;
i++;
}
}
}
public static void PigeonHoleSortDescending(this IList<int> collection)
{
int min = collection.Min();
int max = collection.Max();
int size = max - min + 1;
int[] holes = new int[size];
foreach (int x in collection)
{
holes[x - min]++;
}
int i = 0;
for (int count = size-1; count >= 0; count--)
{
while (holes[count]-- >0)
{
collection[i] = count + min;
i++;
}
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。